1# XML Parsing 2 3 4Data 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). 5 6 7The **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. 8 9 10**Table 1** XML parsing options 11 12| Name| Type| Mandatory| Description| 13| -------- | -------- | -------- | -------- | 14| supportDoctype | boolean | No| Whether to ignore the document type. The default value is **false**, indicating that the document type is parsed.| 15| ignoreNameSpace | boolean | No| Whether to ignore the namespace. The default value is **false**, indicating that the namespace is parsed.| 16| 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.| 17| 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.| 18| 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.| 19 20 21## Precautions 22 23- To ensure successful XML parsing and conversion, the input XML data must comply with the standard format. 24 25- Currently, parsing a given node is not supported. 26 27 28## Parsing XML Tags and Tag Values 29 301. Import the modules. 31 32 ```ts 33 import xml from '@ohos.xml'; 34 import util from '@ohos.util'; // Use the API provided by the util module to encode the file. 35 ``` 36 372. Create an **XmlPullParser** object. 38 39 The **XmlPullParser** object can be created based on an object of the ArrayBuffer or DataView type. 40 41 ```ts 42 let strXml: string = 43 '<?xml version="1.0" encoding="utf-8"?>' + 44 '<note importance="high" logged="true">' + 45 '<title>Play</title>' + 46 '<lens>Work</lens>' + 47 '</note>'; 48 let textEncoder: util.TextEncoder = new util.TextEncoder(); 49 let arrBuffer: Uint8Array = textEncoder.encodeInto(strXml); // Encode the data to prevent garbled characters. 50 // 1. Create an XmlPullParser object based on an object of the ArrayBuffer type. 51 let that: xml.XmlPullParser = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8'); 52 53 // 2. Create an XmlPullParser object based on an object of the DataView type. 54 let dataView: DataView = new DataView(arrBuffer.buffer); 55 let that: xml.XmlPullParser = new xml.XmlPullParser(dataView, 'UTF-8'); 56 ``` 57 583. Customize a callback function. In this example, the tag and tag value are directly printed. 59 60 ```ts 61 let str: string = ''; 62 function func(name: string, value: string): boolean { 63 str = name + value; 64 console.info(str); 65 return true; // The value true means to continue parsing, and false means to stop parsing. 66 } 67 ``` 68 694. Set parsing options and call the **parse()** function. 70 71 ```ts 72 let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tagValueCallbackFunction:func}; 73 that.parse(options); 74 ``` 75 76 The output is as follows: 77 78 ``` 79 note 80 title 81 Play 82 title 83 lens 84 Work 85 lens 86 note 87 ``` 88 89 90 91 92## Parsing XML Attributes and Attribute Values 93 941. Import the modules. 95 96 ```ts 97 import xml from '@ohos.xml'; 98 import util from '@ohos.util'; // Use the API provided by the util module to encode the file. 99 ``` 100 1012. Create an **XmlPullParser** object. 102 103 ```ts 104 let strXml: string = 105 '<?xml version="1.0" encoding="utf-8"?>' + 106 '<note importance="high" logged="true">' + 107 ' <title>Play</title>' + 108 ' <title>Happy</title>' + 109 ' <lens>Work</lens>' + 110 '</note>'; 111 let textEncoder: util.TextEncoder = new util.TextEncoder(); 112 let arrBuffer: Uint8Array = textEncoder.encodeInto(strXml); // Encode the data to prevent garbled characters. 113 let that: xml.XmlPullParser = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8'); 114 ``` 115 1163. Customize a callback function. In this example, the attribute and attribute value are directly printed. 117 118 ```ts 119 let str: string = ''; 120 function func(name: string, value: string): boolean { 121 str += name + ' ' + value + ' '; 122 return true; // The value true means to continue parsing, and false means to stop parsing. 123 } 124 ``` 125 1264. Set parsing options and call the **parse()** function. 127 128 ```ts 129 let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, attributeValueCallbackFunction:func}; 130 that.parse(options); 131 console.info(str); // Print all attributes and their values at a time. 132 ``` 133 134 The output is as follows: 135 ``` 136 importance high logged true // Attributes and attribute values of the note node 137 ``` 138 139 140## Parsing XML Event Types and Element Depths 141 1421. Import the modules. 143 144 ```ts 145 import xml from '@ohos.xml'; 146 import util from '@ohos.util'; // Use the API provided by the util module to encode the file. 147 ``` 148 1492. Create an **XmlPullParser** object. 150 151 ```ts 152 let strXml: string = 153 '<?xml version="1.0" encoding="utf-8"?>' + 154 '<note importance="high" logged="true">' + 155 '<title>Play</title>' + 156 '</note>'; 157 let textEncoder: util.TextEncoder = new util.TextEncoder(); 158 let arrBuffer: Uint8Array = textEncoder.encodeInto(strXml); // Encode the data to prevent garbled characters. 159 let that: xml.XmlPullParser = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8'); 160 ``` 161 1623. Customize a callback function. In this example, the event type and element depth are directly printed. 163 164 ```ts 165 let str: string = ''; 166 function func(name: xml.EventType, value: xml.ParseInfo): boolean { 167 str = name +' ' + value.getDepth(); // getDepth is called to obtain the element depth. 168 console.info(str) 169 return true; // The value true means to continue parsing, and false means to stop parsing. 170 } 171 ``` 172 1734. Set parsing options and call the **parse()** function. 174 175 ```ts 176 let options: xml.ParseOptions = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}; 177 that.parse(options); 178 ``` 179 180 The output is as follows: 181 182 ``` 183 0 0 // 0: <?xml version="1.0" encoding="utf-8"?>. The event type value of START_DOCUMENT is 0. 0: The depth is 0. 184 2 1 // 2: <note importance="high" logged="true">. The event type value of START_TAG is 2. 1: The depth is 1. 185 2 2 // 2: <title>. The event type value of START_TAG is 2. 2: The depth is 2. 186 4 2 // 4: Play. The event type value of TEXT is 4. 2: The depth is 2. 187 3 2 // 3: </title>. The event type value of END_TAG is 3. 2: The depth is 2. 188 3 1 // 3: </note>. The event type value of END_TAG is 3. 1: The depth is 1 (corresponding to <note>). 189 1 0 // 1: The event type value of END_DOCUMENT is 1. 0: The depth is 0. 190 ``` 191 192 193 194 195## Example Scenario 196 197The following uses invoking all parsing options as an example to describe how to parse XML tags, attributes, and event types. 198 199 200```ts 201import xml from '@ohos.xml'; 202import util from '@ohos.util'; 203 204let strXml: string = 205 '<?xml version="1.0" encoding="UTF-8"?>' + 206 '<book category="COOKING">' + 207 '<title lang="en">Everyday</title>' + 208 '<author>Giada</author>' + 209 '</book>'; 210let textEncoder: util.TextEncoder = new util.TextEncoder(); 211let arrBuffer: Uint8Array = textEncoder.encodeInto(strXml); 212let that: xml.XmlPullParser = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8'); 213let str: string = ''; 214 215function tagFunc(name: string, value: string): boolean { 216 str = name + value; 217 console.info('tag-' + str); 218 return true; 219} 220 221function attFunc(name: string, value: string): boolean { 222 str = name + ' ' + value; 223 console.info('attri-' + str); 224 return true; 225} 226 227function tokenFunc(name: xml.EventType, value: xml.ParseInfo): boolean { 228 str = name + ' ' + value.getDepth(); 229 console.info('token-' + str); 230 return true; 231} 232 233let options: xml.ParseOptions = { 234 supportDoctype: true, 235 ignoreNameSpace: true, 236 tagValueCallbackFunction: tagFunc, 237 attributeValueCallbackFunction: attFunc, 238 tokenValueCallbackFunction: tokenFunc 239}; 240that.parse(options); 241``` 242 243The output is as follows: 244 245 ``` 246 tag- 247 token-0 0 248 tag-book 249 attri-category COOKING 250 token-2 1 251 tag-title 252 attri-lang en 253 token-2 2 254 tag-Everyday 255 token-4 2 256 tag-title 257 token-3 2 258 tag-author 259 token-2 2 260 tag-Giada 261 token-4 2 262 tag-author 263 token-3 2 264 tag-book 265 token-3 1 266 tag- 267 token-1 0 268 ``` 269