XML Parsing
When data is transmitted using XML as the carrier, it is often necessary to parse relevant nodes in actual use. This typically involves three types of operations: parsing XML tags and tag values, parsing XML attributes and attribute values, and parsing XML event types and element depths. For instance, in web services, XML underpins the SOAP protocol. SOAP messages, usually encapsulated in XML format, contain request and response parameters. By parsing these XML messages, web services can process client requests and generate corresponding responses.
The XML module provides the XmlPullParser class for parsing XML files. The input is an ArrayBuffer or DataView containing XML text, and the output is the parsed information.
Table 1: XML Parsing Options. For detailed information, please refer to ParseOptions.
Name | Type | Required | Description |
---|---|---|---|
supportDoctype | boolean | No | Whether to ignore the document type. Default is false, meaning the document type is parsed. |
ignoreNameSpace | boolean | No | Whether to ignore namespaces. Default is false, meaning namespaces are parsed. |
tagValueCallbackFunction | (name: string, value: string) => boolean | No | Callback function for obtaining tagValue, which prints the tag and its value. Default is null, indicating that XML tags and values are not parsed. |
attributeValueCallbackFunction | (name: string, value: string) => boolean | No | Callback function for obtaining attributeValue, which prints the attribute and its value. Default is null, indicating that XML attributes and values are not parsed. |
tokenValueCallbackFunction | (eventType: EventType, value: ParseInfo) => boolean | No | Callback function for obtaining tokenValue, which prints the tag event type and the corresponding attributes of parseInfo. Default is null, indicating that XML event types are not parsed. |
Precautions
- Ensure that the input XML data conforms to the standard format for XML parsing and transformation.
- Currently, XML parsing does not support retrieving node values by specified nodes.
Parsing XML Tags and Values
Import the Module
import { xml, util } from '@kit.ArkTS'; // Need to use util module functions for file encoding
Encode the XML File and Call XmlPullParser
You can construct an XmlPullParser object based on ArrayBuffer or DataView (both construction methods yield the same results and either can be chosen).
let strXml: string =
'<?xml version="1.0" encoding="utf-8"?>' +
'<note importance="high" logged="true">' +
'<title>Play</title>' +
'<lens>Work</lens>' +
'</note>';
let textEncoder: util.TextEncoder = new util.TextEncoder();
let arrBuffer: Uint8Array = textEncoder.encodeInto(strXml); // Encode the data to prevent garbled characters when containing Chinese characters
// Method 1: Construct XmlPullParser based on ArrayBuffer
let that: xml.XmlPullParser = new xml.XmlPullParser(arrBuffer.buffer as object as ArrayBuffer, 'UTF-8');
// Method 2: Construct XmlPullParser based on DataView
// let dataView: DataView = new DataView(arrBuffer.buffer as object as ArrayBuffer);
// let that: xml.XmlPullParser = new xml.XmlPullParser(dataView, 'UTF-8');
Define a Custom Callback Function
This example directly prints the tags and their values.
function func(name: string, value: string): boolean {
if (name === 'note') {
console.info(name);
}
if (value === 'Play' || value === 'Work') {
console.info(' ' + value);
}
if (name === 'title' || name === 'lens') {
console.info(' ' + name);
}
return true; // true: Continue parsing, false: Stop parsing
}
Set Parsing Options and Call the parse Function
let options: xml.ParseOptions = { supportDoctype: true, ignoreNameSpace: true, tagValueCallbackFunction: func };
that.parse(options);
Output Result
note
title
Play
title
lens
Work
lens
note
Parsing XML Attributes and Values
Import the Module
import { xml, util } from '@kit.ArkTS'; // Need to use util module functions for file encoding
Encode the XML File and Call XmlPullParser
let strXml: string =
'<?xml version="1.0" encoding="utf-8"?>' +
'<note importance="high" logged="true">' +
' <title>Play</title>' +
' <title>Happy</title>' +
' <lens>Work</lens>' +
'</note>';
let textEncoder: util.TextEncoder = new util.TextEncoder();
let arrBuffer: Uint8Array = textEncoder.encodeInto(strXml); // Encode the data to prevent garbled characters when containing Chinese characters
let that: xml.XmlPullParser = new xml.XmlPullParser(arrBuffer.buffer as object as ArrayBuffer, 'UTF-8');
Define a Custom Callback Function
This example directly prints the attributes and their values.
let str: string = '';
function func(name: string, value: string): boolean {
str += name + ' ' + value + ' ';
return true; // true: Continue parsing, false: Stop parsing
}
Set Parsing Options and Call the parse Function
let options: xml.ParseOptions = { supportDoctype: true, ignoreNameSpace: true, attributeValueCallbackFunction: func };
that.parse(options);
console.info(str); // Print all attributes and their values at once
Output Result
importance high logged true // Attributes and values of the note node
Parsing XML Event Types and Element Depths
Import the Module
import { xml, util } from '@kit.ArkTS'; // Need to use util module functions for file encoding
Encode the XML File and Call XmlPullParser
let strXml: string =
'<?xml version="1.0" encoding="utf-8"?>' +
'<note importance="high" logged="true">' +
'<title>Play</title>' +
'</note>';
let textEncoder: util.TextEncoder = new util.TextEncoder();
let arrBuffer: Uint8Array = textEncoder.encodeInto(strXml); // Encode the data to prevent garbled characters when containing Chinese characters
let that: xml.XmlPullParser = new xml.XmlPullParser(arrBuffer.buffer as object as ArrayBuffer, 'UTF-8');
Define a Custom Callback Function
This example directly prints the element event types and element depths.
let str: string = '';
function func(eventType: xml.EventType, parseInfo: xml.ParseInfo): boolean {
const depth = parseInfo.getDepth(); // getDepth retrieves the current depth of the element
console.info(`${eventType} ${depth}`);
return true; // true: Continue parsing, false: Stop parsing
}
Set Parsing Options and Call the parse Function
let options: xml.ParseOptions = { supportDoctype: true, ignoreNameSpace: true, tokenValueCallbackFunction: func };
that.parse(options);
Output Result
0 0 // 0: <?xml version="1.0" encoding="utf-8"?> corresponds to event type START_DOCUMENT with value 0. Depth starts at 0.
2 1 // 2: <note importance="high" logged="true"> corresponds to event type START_TAG with value 2. Depth is 1.
2 2 // 2: <title> corresponds to event type START_TAG with value 2. Depth is 2.
4 2 // 4: Play corresponds to event type TEXT with value 4. Depth is 2.
3 2 // 3: </title> corresponds to event type END_TAG with value 3. Depth is 2.
3 1 // 3: </note> corresponds to event type END_TAG with value 3. Depth is 1 (corresponding to <note>).
1 0 // 1: corresponds to event type END_DOCUMENT with value 1.
Top comments (0)