OpenHarmony语言基础类库【@ohos.xml (xml解析与生成)】

2024-04-29 06:44

本文主要是介绍OpenHarmony语言基础类库【@ohos.xml (xml解析与生成)】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 将XML文本转换为JavaScript对象、以及XML文件生成和解析的一系列接口。

说明:

本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import xml from '@ohos.xml';

XmlSerializer

XmlSerializer接口用于生成XML文件。

constructor

constructor(buffer: ArrayBuffer | DataView, encoding?: string)

XmlSerializer的构造函数。

系统能力:  SystemCapability.Utils.Lang

参数:

参数名类型必填说明
bufferArrayBufferDataView用于接收写入xml信息的ArrayBuffer或DataView内存。
encodingstring编码格式 , 默认'utf-8'(目前仅支持'utf-8')。

鸿蒙开发指导文档:gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md点击或者复制转到。

示例:

let arrayBuffer = new ArrayBuffer(2048);
let thatSer = new xml.XmlSerializer(arrayBuffer, "utf-8");
thatSer.setDeclaration();
let result = '<?xml version="1.0" encoding="utf-8"?>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(view1) // <?xml version="1.0" encoding="utf-8"?>

setAttributes

setAttributes(name: string, value: string): void

设置Attributes方法。

系统能力:  SystemCapability.Utils.Lang

参数:

参数名类型必填说明
namestring属性的key值。
valuestring属性的value值。

示例:

const MY_MAX = 2048;
let arrayBuffer = new ArrayBuffer(MY_MAX);
let thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.startElement("note");
thatSer.setAttributes("importance1", "high1");
thatSer.endElement();
let result = '<note importance1="high1"/>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(view1) // <note importance1="high1"/>

addEmptyElement

addEmptyElement(name: string): void

写入一个空元素。

系统能力:  SystemCapability.Utils.Lang

参数:

参数名类型必填说明
namestring该空元素的元素名。

示例:

const MY_MAX = 2048;
let arrayBuffer = new ArrayBuffer(MY_MAX);
let thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.addEmptyElement("d");
let result = '<d/>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(view1) // <d/>

setDeclaration

setDeclaration(): void

编写带有编码的文件声明。

系统能力:  SystemCapability.Utils.Lang

示例:

const MY_MAX = 2048;
let arrayBuffer = new ArrayBuffer(MY_MAX);
let thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.setDeclaration();
thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
thatSer.startElement("note");
thatSer.endElement();
let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(view1)
// <?xml version="1.0" encoding="utf-8"?>
// <h:note xmlns:h="http://www.w3.org/TR/html4/"/>

startElement

startElement(name: string): void

根据给定名称写入元素开始标记。

系统能力:  SystemCapability.Utils.Lang

参数:

参数名类型必填说明
namestring当前元素的元素名。

示例:

const MY_MAX = 2048;
let arrayBuffer = new ArrayBuffer(MY_MAX);
let thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.setDeclaration();
thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
thatSer.startElement("note");
thatSer.endElement();
let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(JSON.stringify(view1)) // <?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>

endElement

endElement(): void

写入元素结束标记。

系统能力:  SystemCapability.Utils.Lang

示例:

const MY_MAX = 2048;
let arrayBuffer = new ArrayBuffer(MY_MAX);
let thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.setDeclaration();
thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
thatSer.startElement("note");
thatSer.endElement();
let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(JSON.stringify(view1)) // <?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>

setNamespace

setNamespace(prefix: string, namespace: string): void

写入当前元素标记的命名空间。

系统能力:  SystemCapability.Utils.Lang

参数:

参数名类型必填说明
prefixstring当前元素及其子元素的前缀。
namespacestring当前元素及其子元素的命名空间。

示例:

const MY_MAX = 2048;
let arrayBuffer = new ArrayBuffer(MY_MAX);
let thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.setDeclaration();
thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
thatSer.startElement("note");
thatSer.endElement();
let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(JSON.stringify(view1)) // <?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>

setComment

setComment(text: string): void

写入comment属性。

系统能力:  SystemCapability.Utils.Lang

参数:

参数名类型必填说明
textstring当前元素的注释内容。

示例:

const MY_MAX = 2048;
let arrayBuffer = new ArrayBuffer(MY_MAX);
let thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.setComment("Hello, World!");
let result = '<!--Hello, World!-->';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(view1) // <!--Hello, World!-->

setCDATA

setCDATA(text: string): void

写入CDATA属性。

系统能力:  SystemCapability.Utils.Lang

参数:

参数名类型必填说明
textstringCDATA属性的内容。

示例:

const MY_MAX = 2048;
let arrayBuffer = new ArrayBuffer(MY_MAX);
let thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.setCDATA('root SYSTEM')
let result = '<![CDATA[root SYSTEM]]>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(view1) // <![CDATA[root SYSTEM]]>

[](zh-cn/application-dev/reference/apis/js-apis-xml.md · OpenHarmony/docs - Gitee.com)setText

setText(text: string): void

设置Text方法。

系统能力:  SystemCapability.Utils.Lang

参数:

参数名类型必填说明
textstringtext属性的内容。

示例:

const MY_MAX = 2048;
let arrayBuffer = new ArrayBuffer(MY_MAX);
let thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.startElement("note");
thatSer.setAttributes("importance", "high");
thatSer.setText("Happy1");
thatSer.endElement();
let result = '<note importance="high">Happy1</note>';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(view1) // <note importance="high">Happy1</note>

setDocType

setDocType(text: string): void

写入DocType属性。

系统能力:  SystemCapability.Utils.Lang

参数:

参数名类型必填说明
textstringDocType属性的内容。

示例:

const MY_MAX = 2048;
let arrayBuffer = new ArrayBuffer(MY_MAX);
let thatSer = new xml.XmlSerializer(arrayBuffer);
thatSer.setDocType('root SYSTEM "http://www.test.org/test.dtd"');
let result = '<!DOCTYPE root SYSTEM "http://www.test.org/test.dtd">';
let view = new Uint8Array(arrayBuffer);
let view1 = "";
for (let i = 0; i < result.length; ++i) {view1 = view1 + String.fromCodePoint(view[i]);
}
console.log(view1) // <!DOCTYPE root SYSTEM "http://www.test.org/test.dtd">

XmlPullParser

XmlPullParser接口用于解析现有的XML文件。

constructor

constructor(buffer: ArrayBuffer | DataView, encoding?: string)

构造并返回一个XmlPullParser对象。

系统能力:  SystemCapability.Utils.Lang

参数:

参数名类型必填说明
bufferArrayBufferDataView需要解析的xml文本信息。
encodingstring编码格式 , 默认'utf-8'(目前仅支持'utf-8')。

示例:

let strXml ='<?xml version="1.0" encoding="utf-8"?>' +'<!DOCTYPE note [\n<!ENTITY foo "baa">]>' +'<note importance="high" logged="true">' +'    <![CDATA[\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n]]>' +'    <!--Hello, World!-->' +'    <company>John &amp; Hans</company>' +'    <title>Happy</title>' +'    <title>Happy</title>' +'    <lens>Work</lens>' +'    <lens>Play</lens>' +'    <?go there?>' +'    <a><b/></a>' +'    <h:table xmlns:h="http://www.w3.org/TR/html4/">' +'        <h:tr>' +'            <h:td>Apples</h:td>' +'            <h:td>Bananas</h:td>' +'        </h:tr>' +'    </h:table>' +'</note>';
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer, 'UTF-8');
let str1 = '';
function func1(name, value){str1 += name+value;return true;
}
let options = {supportDoctype:true, ignoreNameSpace:true, tagValueCallbackFunction:func1}
that.parse(options);
console.log(str1)
//  note [<!ENTITY foo "baa">]note    funcrion matchwo(a,6){return 1;}    Hello, World!    companyJohn amp;amp; Hanscompany    titleHappytitle    titleHappytitle    lensWorklens    lensPlaylens    go there    abba    h:table        h:tr            h:tdApplesh:td            h:tdBananash:td        h:tr    h:tablenote

parse

parse(option: ParseOptions): void

该接口用于解析xml。

系统能力:  SystemCapability.Utils.Lang

参数:

参数名类型必填说明
option[ParseOptions]用户控制以及获取解析信息的选项。

示例:

let strXml ='<?xml version="1.0" encoding="utf-8"?>' +'<note importance="high" logged="true">' +'    <title>Happy</title>' +'    <todo>Work</todo>' +'    <todo>Play</todo>' +'</note>';
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){arrTag[i] = 'key:'+key+' value:'+ value.getDepth();str += arrTag[i];i++;return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:0key:2 value:1key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:3 value:1key:1 value:0
// 解析:
// key代表了当前事件类型,value为当前解析的深度。你可以根据EVENTTYPE来知道具体的解析事件。例如本示例结果key: value代表含义为:
// 0(START_DOCUMENT):0(起始深度为0), 2(START_TAG):1(解析到开始标签node, 对应深度为1), 10(WHITESPACE):1(解析到空白标签空格, 对应深度为1), 2(START_TAG):2(解析到开始标签title, 对应深度为2), ...

ParseOptions

xml解析选项。

系统能力:  以下各项对应的系统能力均为SystemCapability.Utils.Lang

名称类型必填说明
supportDoctypeboolean是否忽略Doctype , 默认false。
ignoreNameSpaceboolean是否忽略NameSpace,默认false。
tagValueCallbackFunction(name: string, value: string) => boolean获取tagValue回调函数 , 默认null。
attributeValueCallbackFunction(name: string, value: string) => boolean获取attributeValue回调函数 , 默认null。
tokenValueCallbackFunction(eventType: [EventType], value: [ParseInfo]) => boolean获取tokenValue回调函数, 默认null。

ParseInfo

当前xml解析信息。

getColumnNumber

getColumnNumber(): number

获取当前列号,从1开始。

系统能力:  SystemCapability.Utils.Lang

返回值:

类型说明
number返回当前列号。

示例:

let strXml ='<?xml version="1.0" encoding="utf-8"?>' +'<note importance="high" logged="true">' +'    <title>Happy</title>' +'    <todo>Work</todo>' +'    <todo>Play</todo>' +'</note>';
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){arrTag[i] = 'key:'+key+' value:'+ value.getColumnNumber();str += arrTag[i];i++;return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:1key:2 value:77key:10 value:81key:2 value:88key:4 value:93key:3 value:101key:10 value:105key:2 value:111key:4 value:115key:3 value:122key:10 value:126key:2 value:132key:4 value:136key:3 value:143key:3 value:150key:1 value:299

getDepth

getDepth(): number

获取元素的当前深度。

系统能力:  SystemCapability.Utils.Lang

返回值:

类型说明
number返回元素的当前深度。

示例:

let strXml ='<?xml version="1.0" encoding="utf-8"?>' +'<note importance="high" logged="true">' +'    <title>Happy</title>' +'    <todo>Work</todo>' +'    <todo>Play</todo>' +'</note>';
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){arrTag[i] = 'key:'+key+' value:'+ value.getDepth();str += arrTag[i];i++;return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:0key:2 value:1key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:10 value:1key:2 value:2key:4 value:2key:3 value:2key:3 value:1key:1 value:0
// 解析:
// key代表了当前事件类型,value为当前解析的深度。你可以根据EVENTTYPE来知道具体的解析事件。例如本示例结果key: value代表含义为:
// 0(START_DOCUMENT):0(起始深度为0), 2(START_TAG):1(解析到开始标签node, 对应深度为1), 10(WHITESPACE):1(解析到空白标签空格, 对应深度为1), 2(START_TAG):2(解析到开始标签title, 对应深度为2), ...

getLineNumber

getLineNumber(): number

获取当前行号,从1开始。

系统能力:  SystemCapability.Utils.Lang

返回值:

类型说明
number返回当前行号。

示例:

let strXml ='<?xml version="1.0" encoding="utf-8"?>' +'<note importance="high" logged="true">' +'    <title>Happy</title>' +'    <todo>Work</todo>' +'    <todo>Play</todo>' +'</note>';
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){arrTag[i] = 'key:'+key+' value:'+ value.getLineNumber();str += arrTag[i];i++;return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:1key:2 value:1key:10 value:1key:2 value:1key:4 value:1key:3 value:1key:10 value:1key:2 value:1key:4 value:1key:3 value:1key:10 value:1key:2 value:1key:4 value:1key:3 value:1key:3 value:1key:1 value:1

getName

getName(): string

获取当前元素名称。

系统能力:  SystemCapability.Utils.Lang

返回值:

类型说明
string返回当前元素名称。

示例:

let strXml ='<?xml version="1.0" encoding="utf-8"?>' +'<note importance="high" logged="true">' +'    <title>Happy</title>' +'    <todo>Work</todo>' +'    <todo>Play</todo>' +'</note>';
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){arrTag[i] = 'key:'+key+' value:'+ value.getName();str += arrTag[i];i++;return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:key:2 value:notekey:10 value:key:2 value:titlekey:4 value:key:3 value:titlekey:10 value:key:2 value:todokey:4 value:key:3 value:todokey:10 value:key:2 value:todokey:4 value:key:3 value:todokey:3 value:notekey:1 value:

getNamespace

getNamespace(): string

获取当前元素的命名空间。

系统能力:  SystemCapability.Utils.Lang

返回值:

类型说明
string返回当前元素的命名空间。

示例:

let strXml ='<?xml version="1.0" encoding="utf-8"?>' +'<note importance="high" logged="true">' +'    <title>Happy</title>' +'    <todo>Work</todo>' +'    <todo>Play</todo>' +'</note>';
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){arrTag[i] = 'key:'+key+' value:'+ value.getNamespace();str += arrTag[i];i++;return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:key:2 value:key:10 value:key:2 value:key:4 value:key:3 value:key:10 value:key:2 value:key:4 value:key:3 value:key:10 value:key:2 value:key:4 value:key:3 value:key:3 value:key:1 value:

getPrefix

getPrefix(): string

获取当前元素前缀。

系统能力:  SystemCapability.Utils.Lang

返回值:

类型说明
string返回当前元素前缀。

示例:

let strXml ='<?xml version="1.0" encoding="utf-8"?>' +'<note importance="high" logged="true">' +'    <title>Happy</title>' +'    <todo>Work</todo>' +'    <todo>Play</todo>' +'</note>';
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){arrTag[i] = 'key:'+key+' value:'+ value.getPrefix();str += arrTag[i];i++;return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:key:2 value:key:10 value:key:2 value:key:4 value:key:3 value:key:10 value:key:2 value:key:4 value:key:3 value:key:10 value:key:2 value:key:4 value:key:3 value:key:3 value:key:1 value:

getText

getText(): string

获取当前事件的文本内容。

系统能力:  SystemCapability.Utils.Lang

返回值:

类型说明
string返回当前事件的文本内容。

示例:

let strXml ='<?xml version="1.0" encoding="utf-8"?>' +'<note importance="high" logged="true">' +'    <title>Happy</title>' +'    <todo>Work</todo>' +'    <todo>Play</todo>' +'</note>';
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){arrTag[i] = 'key:'+key+' value:'+ value.getText();str += arrTag[i];i++;return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:key:2 value:key:10 value:    key:2 value:key:4 value:Happykey:3 value:key:10 value:    key:2 value:key:4 value:Workkey:3 value:key:10 value:    key:2 value:key:4 value:Playkey:3 value:key:3 value:key:1 value:

isEmptyElementTag

isEmptyElementTag(): boolean

判断当前元素是否为空元素。

系统能力:  SystemCapability.Utils.Lang

返回值:

类型说明
boolean返回true,当前元素为空元素。

示例:

let strXml ='<?xml version="1.0" encoding="utf-8"?>' +'<note importance="high" logged="true">' +'    <title>Happy</title>' +'    <todo>Work</todo>' +'    <todo>Play</todo>' +'</note>';
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){arrTag[i] = 'key:'+key+' value:'+ value.isEmptyElementTag();str += arrTag[i];i++;return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:falsekey:2 value:falsekey:10 value:falsekey:2 value:falsekey:4 value:falsekey:3 value:falsekey:10 value:falsekey:2 value:falsekey:4 value:falsekey:3 value:falsekey:10 value:falsekey:2 value:falsekey:4 value:falsekey:3 value:falsekey:3 value:falsekey:1 value:false

isWhitespace

isWhitespace(): boolean

判断当前文本事件是否仅包含空格字符。

系统能力:  SystemCapability.Utils.Lang

返回值:

类型说明
boolean返回true,当前文本事件仅包含空格字符。

示例:

let strXml ='<?xml version="1.0" encoding="utf-8"?>' +'<note importance="high" logged="true">' +'    <title>Happy</title>' +'    <todo>Work</todo>' +'    <todo>Play</todo>' +'</note>';
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){arrTag[i] = 'key:'+key+' value:'+ value.isWhitespace();str += arrTag[i];i++;return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:truekey:2 value:falsekey:10 value:truekey:2 value:truekey:4 value:falsekey:3 value:truekey:10 value:truekey:2 value:truekey:4 value:falsekey:3 value:truekey:10 value:truekey:2 value:truekey:4 value:falsekey:3 value:truekey:3 value:truekey:1 value:true

getAttributeCount

getAttributeCount(): number

获取当前开始标记的属性数。

系统能力:  SystemCapability.Utils.Lang

返回值:

类型说明
number当前开始标记的属性数。

示例:

let strXml ='<?xml version="1.0" encoding="utf-8"?>' +'<note importance="high" logged="true">' +'    <title>Happy</title>' +'    <todo>Work</todo>' +'    <todo>Play</todo>' +'</note>';
let textEncoder = new util.TextEncoder();
let arrbuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrbuffer.buffer);
let arrTag = {};
let str = "";
let i = 0;
function func(key, value){arrTag[i] = 'key:'+key+' value:'+ value.getAttributeCount();str += arrTag[i];i++;return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
}
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
that.parse(options);
console.log(str);
// 输出:
// key:0 value:0key:2 value:2key:10 value:0key:2 value:0key:4 value:0key:3 value:0key:10 value:0key:2 value:0key:4 value:0key:3 value:0key:10 value:0key:2 value:0key:4 value:0key:3 value:0key:3 value:0key:1 value:0

EventType

事件枚举。

系统能力:  以下各项对应的系统能力均为SystemCapability.Utils.Lang

名称说明
START_DOCUMENT0启动文件事件。
END_DOCUMENT1结束文件事件。
START_TAG2启动标签事件。
END_TAG3结束标签事件。
TEXT4文本事件。
CDSECT5CDATA事件。
COMMENT6XML注释事件。
DOCDECL7XML文档类型声明事件。
INSTRUCTION8XML处理指令声明事件。
ENTITY_REFERENCE9实体引用事件。
WHITESPACE10空白事件。

鸿蒙语言有TS、ArkTS等语法,那么除了这些基础知识之外,其核心技术点有那些呢?下面就用一张整理出的鸿蒙学习路线图表示:

从上面的OpenHarmony技术梳理来看,鸿蒙的学习内容也是很多的。现在全网的鸿蒙学习文档也是非常的少,下面推荐一些:完整内容可在头像页保存,或这qr23.cn/AKFP8k甲助力

内容包含:《鸿蒙NEXT星河版开发学习文档》

  • ArkTS
  • 声明式ArkUI
  • 多媒体
  • 通信问题
  • 系统移植
  • 系统裁剪
  • FW层的原理
  • 各种开发调试工具
  • 智能设备开发
  • 分布式开发等等。

这些就是对往后开发者的分享,希望大家多多点赞关注喔!

这篇关于OpenHarmony语言基础类库【@ohos.xml (xml解析与生成)】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/945370

相关文章

MySQL字符串转数值的方法全解析

《MySQL字符串转数值的方法全解析》在MySQL开发中,字符串与数值的转换是高频操作,本文从隐式转换原理、显式转换方法、典型场景案例、风险防控四个维度系统梳理,助您精准掌握这一核心技能,需要的朋友可... 目录一、隐式转换:自动但需警惕的&ld编程quo;双刃剑”二、显式转换:三大核心方法详解三、典型场景

springboot中配置logback-spring.xml的方法

《springboot中配置logback-spring.xml的方法》文章介绍了如何在SpringBoot项目中配置logback-spring.xml文件来进行日志管理,包括如何定义日志输出方式、... 目录一、在src/main/resources目录下,也就是在classpath路径下创建logba

Java使用Spire.Barcode for Java实现条形码生成与识别

《Java使用Spire.BarcodeforJava实现条形码生成与识别》在现代商业和技术领域,条形码无处不在,本教程将引导您深入了解如何在您的Java项目中利用Spire.Barcodefor... 目录1. Spire.Barcode for Java 简介与环境配置2. 使用 Spire.Barco

Java利用Spire.Doc for Java实现在模板的基础上创建Word文档

《Java利用Spire.DocforJava实现在模板的基础上创建Word文档》在日常开发中,我们经常需要根据特定数据动态生成Word文档,本文将深入探讨如何利用强大的Java库Spire.Do... 目录1. Spire.Doc for Java 库介绍与安装特点与优势Maven 依赖配置2. 通过替换

C语言逗号运算符和逗号表达式的使用小结

《C语言逗号运算符和逗号表达式的使用小结》本文详细介绍了C语言中的逗号运算符和逗号表达式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习... 在C语言中逗号“,”也是一种运算符,称为逗号运算符。 其功能是把两个表达式连接其一般形式为:表达

Go语言实现桥接模式

《Go语言实现桥接模式》桥接模式是一种结构型设计模式,它将抽象部分与实现部分分离,使它们可以独立地变化,本文就来介绍一下了Go语言实现桥接模式,感兴趣的可以了解一下... 目录简介核心概念为什么使用桥接模式?应用场景案例分析步骤一:定义实现接口步骤二:创建具体实现类步骤三:定义抽象类步骤四:创建扩展抽象类步

GO语言实现串口简单通讯

《GO语言实现串口简单通讯》本文分享了使用Go语言进行串口通讯的实践过程,详细介绍了串口配置、数据发送与接收的代码实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目录背景串口通讯代码代码块分解解析完整代码运行结果背景最近再学习 go 语言,在某宝用5块钱买了个

SQL 注入攻击(SQL Injection)原理、利用方式与防御策略深度解析

《SQL注入攻击(SQLInjection)原理、利用方式与防御策略深度解析》本文将从SQL注入的基本原理、攻击方式、常见利用手法,到企业级防御方案进行全面讲解,以帮助开发者和安全人员更系统地理解... 目录一、前言二、SQL 注入攻击的基本概念三、SQL 注入常见类型分析1. 基于错误回显的注入(Erro

SpringBoot集成iText快速生成PDF教程

《SpringBoot集成iText快速生成PDF教程》本文介绍了如何在SpringBoot项目中集成iText9.4.0生成PDF文档,包括新特性的介绍、环境准备、Service层实现、Contro... 目录SpringBoot集成iText 9.4.0生成PDF一、iText 9新特性与架构变革二、环

idea-java序列化serialversionUID自动生成方式

《idea-java序列化serialversionUID自动生成方式》Java的Serializable接口用于实现对象的序列化和反序列化,通过将对象转换为字节流来存储或传输,实现Serializa... 目录简介实现序列化serialVersionUID配置使用总结简介Java.io.Seripyth