Implementing XML Parsing in ArkTS for HarmonyOS Applications

The ArkTS framework in HarmonyOS provides robust XML processing capabilities through its @ohos.xml module. This guide demonstrates practical implementation techniques using the XmlPullParser class with updated coding patterns.

Attribute Extraction Workflow

Initialize the parser with properly encoded XML data to hendle international character sets:

import xmlParser from '@ohos.xml';
import textUtil from '@ohos.util';

const xmlContent = `<?xml version="1.0" encoding="UTF-8"?>
<configuration active="true" version="3.1">
  <parameter key="timeout">3000</parameter>
  <parameter key="retries">5</parameter>
</configuration>`;

const encoder = new textUtil.TextEncoder();
const encodedData = encoder.encode(xmlContent);
const parser = new xmlParser.XmlPullParser(
  encodedData.buffer as ArrayBuffer, 
  'UTF-8'
);

Implement attribute processing with a custom handler:

let attributeResults = '';
function processAttribute(name: string, value: string): boolean {
  attributeResults += `${name}→${value} | `;
  return true;
}

const parseConfig = {
  supportDoctype: true,
  ignoreNameSpace: true,
  attributeValueCallbackFunction: processAttribute
};

parser.parse(parseConfig);
console.log(attributeResults); 
// Output: active→true | version→3.1 |

Event Tracking and Depth Analysis

Monitor parsing progression through event types and nesting levels:

const document = `<?xml version="1.0"?>
<container>
  <element>Data</element>
</container>`;

const buffer = new textUtil.TextEncoder().encode(document);
const xmlReader = new xmlParser.XmlPullParser(buffer.buffer as ArrayBuffer);

function trackEvents(eventType: xmlParser.EventType, context: xmlParser.ParseInfo) {
  console.log(`Event: ${eventType}, Level: ${context.getDepth()}`);
  return true;
}

xmlReader.parse({
  tokenValueCallbackFunction: trackEvents
});

Execution yields sequential event tracking:

Event: 0, Level: 0  // Document start
Event: 2, Level: 1  // <container> open
Event: 2, Level: 2  // <element> open
Event: 4, Level: 2  // Text content
Event: 3, Level: 2  // </element> close
Event: 3, Level: 1  // </container> close
Event: 1, Level: 0  // Document end

Integrated Parsing Implemantation

Combine multiple parsing handlers for comprehensive XML analysis:

const catalog = `<?xml version="1.0"?>
<products category="electronics">
  <item id="A100" stock="in">
    <name>Wireless Headset</name>
  </item>
</products>`;

const dataBuffer = new textUtil.TextEncoder().encode(catalog);
const xmlEngine = new xmlParser.XmlPullParser(dataBuffer.buffer as ArrayBuffer);

function handleTag(name: string, content: string) {
  console.log(`Tag: ${name}${content ? ` → ${content}` : ''}`);
}

function handleAttributes(key: string, val: string) {
  console.log(`Attribute: ${key} = ${val}`);
}

function handleTokens(event: xmlParser.EventType, info: xmlParser.ParseInfo) {
  console.log(`Token: ${event} @ Depth ${info.getDepth()}`);
}

xmlEngine.parse({
  tagValueCallbackFunction: handleTag,
  attributeValueCallbackFunction: handleAttributes,
  tokenValueCallbackFunction: handleTokens
});

Output demonstrates coordinated event handling:

Token: 0 @ Depth 0
Tag: products
Attribute: category = electronics
Token: 2 @ Depth 1
Tag: item
Attribute: id = A100
Attribute: stock = in
Token: 2 @ Depth 2
Tag: name → Wireless Headset
Token: 4 @ Depth 2
Tag: name
Token: 3 @ Depth 2
Tag: item
Token: 3 @ Depth 1
Tag: products
Token: 1 @ Depth 0

Tags: ArkTS HarmonyOS XML Parsing XmlPullParser

Posted on Wed, 04 Nov 2026 16:23:15 +0000 by The-Last-Escape