xml-parsing.md 8.3 KB
Newer Older
G
Gloria 已提交
1 2 3 4 5 6
# XML Parsing


Data transferred in XML format must be parsed in actual use. Generally, three types of elements need to be parsed, as described in [Parsing XML Tags and Tag Values](#parsing-xml-tags-and-tag-values), [Parsing XML Attributes and Attribute Values](#parsing-xml-attributes-and-attribute-values), and [Parsing XML Event Types and Element Depths](#parsing-xml-event-types-and-element-depths).


G
Gloria 已提交
7
The **xml** module provides the **XmlPullParser** class to parse XML files. The input is an object of the ArrayBuffer or DataView type containing XML text, and the output is the parsed information.
G
Gloria 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271


**Table 1** XML parsing options

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| supportDoctype | boolean | No| Whether to ignore the document type. The default value is **false**, indicating that the document type is parsed.|
| ignoreNameSpace | boolean | No| Whether to ignore the namespace. The default value is **false**, indicating that the namespace is parsed.|
| tagValueCallbackFunction | (name: string, value: string) => boolean | No| Callback used to return **tagValue**, which consists of a tag and its value. The default value is **null**, indicating that XML tags and tag values are not parsed.|
| attributeValueCallbackFunction | (name: string, value: string) => boolean | No| Callback used to return **attributeValue**, which consists of an attribute and its value. The default value is **null**, indicating that XML attributes and attribute values are not parsed.|
| tokenValueCallbackFunction | (eventType: EventType, value: ParseInfo) => boolean | No| Callback used to return **tokenValue**, which consists of the event type and the attributes of **parseInfo**. The default value is **null**, indicating that the event type and the attributes of **parseInfo** are not parsed.|


## Precautions

- To ensure successful XML parsing and conversion, the input XML data must comply with the standard format.

- Currently, parsing a given node is not supported.


## Parsing XML Tags and Tag Values

1. Import the modules.
   
   ```js
   import xml from '@ohos.xml';
   import util from '@ohos.util'; // Use the API provided by the util module to encode the file.
   ```

2. Create an **XmlPullParser** object.

   The **XmlPullParser** object can be created based on an object of the ArrayBuffer or DataView type.

   
   ```js
   let strXml =
     '<?xml version="1.0" encoding="utf-8"?>' +
       '<note importance="high" logged="true">' +
       '<title>Play</title>' +
       '<lens>Work</lens>' +
       '</note>';
   let textEncoder = new util.TextEncoder();
   let arrBuffer = textEncoder.encodeInto(strXml); // Encode the data to prevent garbled characters.
   // 1. Create an XmlPullParser object based on an object of the ArrayBuffer type.
   let that = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8');
   
   // 2. Create an XmlPullParser object based on an object of the DataView type.
   let dataView = new DataView(arrBuffer.buffer);
   let that = new xml.XmlPullParser(dataView, 'UTF-8');
   ```

3. Customize a callback function. In this example, the tag and tag value are directly printed.
   
   ```js
   let str = '';
   function func(name, value){
     str = name + value;
     console.info(str);
     return true; // The value true means to continue parsing, and false means to stop parsing.
   }
   ```

4. Set parsing options and call the **parse()** function.
   
   ```js
   let options = {supportDoctype:true, ignoreNameSpace:true, tagValueCallbackFunction:func};
   that.parse(options);
   ```

   The output is as follows:

   
   ```js
   note
   title
   Play
   title
   lens
   Work
   lens
   note
   ```


## Parsing XML Attributes and Attribute Values

1. Import the modules.
   
   ```js
   import xml from '@ohos.xml';
   import util from '@ohos.util'; // Use the API provided by the util module to encode the file.
   ```

2. Create an **XmlPullParser** object.
   
   ```js
   let strXml =
     '<?xml version="1.0" encoding="utf-8"?>' +
       '<note importance="high" logged="true">' +
       '    <title>Play</title>' +
       '    <title>Happy</title>' +
       '    <lens>Work</lens>' +
       '</note>';
   let textEncoder = new util.TextEncoder();
   let arrBuffer = textEncoder.encodeInto(strXml); // Encode the data to prevent garbled characters.
   let that = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8');
   ```

3. Customize a callback function. In this example, the attribute and attribute value are directly printed.
   
   ```js
   let str = '';
   function func(name, value){
     str += name + ' ' + value + ' ';
     return true; // The value true means to continue parsing, and false means to stop parsing.
   }
   ```

4. Set parsing options and call the **parse()** function.
   
   ```js
   let options = {supportDoctype:true, ignoreNameSpace:true, attributeValueCallbackFunction:func};
   that.parse(options);
   console.info(str); // Print all attributes and their values at a time.
   ```

   The output is as follows:

   
   ```js
   importance high logged true // Attributes and attribute values of the note node
   ```


## Parsing XML Event Types and Element Depths

1. Import the modules.
   
   ```js
   import xml from '@ohos.xml';
   import util from '@ohos.util'; // Use the API provided by the util module to encode the file.
   ```

2. Create an **XmlPullParser** object.
   
   ```js
   let strXml =
     '<?xml version="1.0" encoding="utf-8"?>' +
     '<note importance="high" logged="true">' +
     '<title>Play</title>' +
     '</note>';
   let textEncoder = new util.TextEncoder();
   let arrBuffer = textEncoder.encodeInto(strXml); // Encode the data to prevent garbled characters.
   let that = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8');
   ```

3. Customize a callback function. In this example, the event type and element depth are directly printed.
   
   ```js
   let str = '';
   function func(name, value){
     str = name +' ' + value.getDepth(); // getDepth is called to obtain the element depth.
     console.info(str)
     return true; // The value true means to continue parsing, and false means to stop parsing.
   }
   ```

4. Set parsing options and call the **parse()** function.
   
   ```js
   let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func};
   that.parse(options);
   ```

   The output is as follows:

   
   ```js
   0 0 // 0: <?xml version="1.0" encoding="utf-8"?>. The event type value of START_DOCUMENT is 0. 0: The depth is 0.
   2 1 // 2: <note importance="high" logged="true">. The event type value of START_TAG is 2. 1: The depth is 1.
   2 2 // 2: <title>. The event type value of START_TAG is 2. 2: The depth is 2.
   4 2 // 4: Play. The event type value of TEXT is 4. 2: The depth is 2.
   3 2 // 3: </title>. The event type value of END_TAG is 3. 2: The depth is 2.
   3 1 // 3: </note>. The event type value of END_TAG is 3. 1: The depth is 1 (corresponding to <note>).
   1 0 // 1: The event type value of END_DOCUMENT is 1. 0: The depth is 0.
   ```


## Example Scenario

The following uses invoking all parsing options as an example to describe how to parse XML tags, attributes, and event types.


```js
import xml from '@ohos.xml';
import util from '@ohos.util';

let strXml =
  '<?xml version="1.0" encoding="UTF-8"?>' +
    '<book category="COOKING">' +
    '<title lang="en">Everyday</title>' +
    '<author>Giada</author>' +
    '</book>';
let textEncoder = new util.TextEncoder();
let arrBuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8');
let str = '';

function tagFunc(name, value) {
  str = name + value;
  console.info('tag-' + str);
  return true;
}

function attFunc(name, value) {
  str = name + ' ' + value;
  console.info('attri-' + str);
  return true;
}

function tokenFunc(name, value) {
  str = name + ' ' + value.getDepth();
  console.info('token-' + str);
  return true;
}

let options = {
  supportDocType: true,
  ignoreNameSpace: true,
  tagValueCallbackFunction: tagFunc,
  attributeValueCallbackFunction: attFunc,
  tokenValueCallbackFunction: tokenFunc
};
that.parse(options);

```

The output is as follows:


```js
tag-
token-0 0
tag-book
attri-category COOKING
token-2 1
tag-title
attri-lang en
token-2 2
tag-Everyday
token-4 2
tag-title
token-3 2
tag-author
token-2 2
tag-Giada
token-4 2
tag-author
token-3 2
tag-book
token-3 1
tag-
token-1 0
```