Working with XML in HarmonyOS ArkTS: Structure Definition and Processing

An XML element consists of three main parts:

  • Tag: Marks the begining and end of an element
  • Attributes: Provide additional information about the element
  • Content: The data contained within the element

Element Structure Example

<book category="fiction">
  <title>XML Fundamentals</title>
  <author>Jane Smith</author>
</book>

Document Structure Definition Methods

XML Schema Approach

Create a schema definition file (XSD) to validate XML structure:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="book">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="title" type="xs:string"/>
        <xs:element name="author" type="xs:string"/>
      </xs:sequence>
      <xs:attribute name="category" type="xs:string" use="required"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

DTD Approach

Alternative method using Document Type Definition:


  <!ELEMENT book (title, author, price)>
  <!ELEMENT title (#PCDATA)>
  <!ELEMENT author (#PCDATA)>
  <!ELEMENT price (#PCDATA)>
  <!ATTLIST book 
    id ID #IMPLIED
    category CDATA #REQUIRED
  >
]>

XML Generatino in ArkTS

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

const createXmlDocument = () => {
  const buffer = new ArrayBuffer(2048);
  const serializer = new xml.XmlSerializer(buffer);
  
  serializer.setDeclaration();
  serializer.startElement('library');
  serializer.startElement('book');
  serializer.setAttributes('genre', 'technical');
  serializer.startElement('title');
  serializer.setText('ArkTS Programming');
  serializer.endElement();
  serializer.startElement('author');
  serializer.setText('Tech Writer');
  serializer.endElement();
  serializer.endElement();
  serializer.endElement();

  const view = new Uint8Array(buffer);
  const decoder = util.TextDecoder.create();
  return decoder.decodeWithStream(view);
};

console.log(createXmlDocument());

XML Parsing Techniques

Basic Element Parsing

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

const parseXmlContent = (xmlString) => {
  const encoder = new util.TextEncoder();
  const buffer = encoder.encodeInto(xmlString).buffer;
  const parser = new xml.XmlPullParser(buffer, 'UTF-8');
  
  const results = [];
  const elementHandler = (name, value) => {
    results.push(`${name}: ${value}`);
    return true;
  };
  
  const options = {
    supportDoctype: true,
    ignoreNameSpace: true,
    tagValueCallbackFunction: elementHandler
  };
  
  parser.parse(options);
  return results;
};

Attribute Parsing

const parseXmlAttributes = (xmlString) => {
  const encoder = new util.TextEncoder();
  const buffer = encoder.encodeInto(xmlString).buffer;
  const parser = new xml.XmlPullParser(buffer, 'UTF-8');
  
  const attributes = [];
  const attrHandler = (name, value) => {
    attributes.push(`${name} = ${value}`);
    return true;
  };
  
  const options = {
    supportDoctype: true,
    ignoreNameSpace: true,
    attributeValueCallbackFunction: attrHandler
  };
  
  parser.parse(options);
  return attributes;
};

Document Structure Aanlysis

const analyzeXmlStructure = (xmlString) => {
  const encoder = new util.TextEncoder();
  const buffer = encoder.encodeInto(xmlString).buffer;
  const parser = new xml.XmlPullParser(buffer, 'UTF-8');
  
  const structure = [];
  const tokenHandler = (name, value) => {
    structure.push(`Event: ${name}, Depth: ${value.getDepth()}`);
    return true;
  };
  
  const options = {
    supportDoctype: true,
    ignoreNameSpace: true,
    tokenValueCallbackFunction: tokenHandler
  };
  
  parser.parse(options);
  return structure;
};

Tags: HarmonyOS ArkTS XML XMLSchema DTD

Posted on Fri, 24 Jul 2026 16:39:15 +0000 by bagsobrands