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

相关文章

C语言中联合体union的使用

本文编辑整理自: http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=179471 一、前言 “联合体”(union)与“结构体”(struct)有一些相似之处。但两者有本质上的不同。在结构体中,各成员有各自的内存空间, 一个结构变量的总长度是各成员长度之和。而在“联合”中,各成员共享一段内存空间, 一个联合变量

RedHat运维-Linux文本操作基础-AWK进阶

你不用整理,跟着敲一遍,有个印象,然后把它保存到本地,以后要用再去看,如果有了新东西,你自个再添加。这是我参考牛客上的shell编程专项题,只不过换成了问答的方式而已。不用背,就算是我自己亲自敲,我现在好多也记不住。 1. 输出nowcoder.txt文件第5行的内容 2. 输出nowcoder.txt文件第6行的内容 3. 输出nowcoder.txt文件第7行的内容 4. 输出nowcode

解析 XML 和 INI

XML 1.TinyXML库 TinyXML是一个C++的XML解析库  使用介绍: https://www.cnblogs.com/mythou/archive/2011/11/27/2265169.html    使用的时候,只要把 tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.

Vim使用基础篇

本文内容大部分来自 vimtutor,自带的教程的总结。在终端输入vimtutor 即可进入教程。 先总结一下,然后再分别介绍正常模式,插入模式,和可视模式三种模式下的命令。 目录 看完以后的汇总 1.正常模式(Normal模式) 1.移动光标 2.删除 3.【:】输入符 4.撤销 5.替换 6.重复命令【. ; ,】 7.复制粘贴 8.缩进 2.插入模式 INSERT

零基础STM32单片机编程入门(一)初识STM32单片机

文章目录 一.概要二.单片机型号命名规则三.STM32F103系统架构四.STM32F103C8T6单片机启动流程五.STM32F103C8T6单片机主要外设资源六.编程过程中芯片数据手册的作用1.单片机外设资源情况2.STM32单片机内部框图3.STM32单片机管脚图4.STM32单片机每个管脚可配功能5.单片机功耗数据6.FALSH编程时间,擦写次数7.I/O高低电平电压表格8.外设接口

大语言模型(LLMs)能够进行推理和规划吗?

大语言模型(LLMs),基本上是经过强化训练的 n-gram 模型,它们在网络规模的语言语料库(实际上,可以说是我们文明的知识库)上进行了训练,展现出了一种超乎预期的语言行为,引发了我们的广泛关注。从训练和操作的角度来看,LLMs 可以被认为是一种巨大的、非真实的记忆库,相当于为我们所有人提供了一个外部的系统 1(见图 1)。然而,它们表面上的多功能性让许多研究者好奇,这些模型是否也能在通常需要系

ps基础入门

1.基础      1.1新建文件      1.2创建指定形状      1.4移动工具          1.41移动画布中的任意元素          1.42移动画布          1.43修改画布大小          1.44修改图像大小      1.5框选工具      1.6矩形工具      1.7图层          1.71图层颜色修改          1

android 带与不带logo的二维码生成

该代码基于ZXing项目,这个网上能下载得到。 定义的控件以及属性: public static final int SCAN_CODE = 1;private ImageView iv;private EditText et;private Button qr_btn,add_logo;private Bitmap logo,bitmap,bmp; //logo图标private st

人工和AI大语言模型成本对比 ai语音模型

这里既有AI,又有生活大道理,无数渺小的思考填满了一生。 上一专题搭建了一套GMM-HMM系统,来识别连续0123456789的英文语音。 但若不是仅针对数字,而是所有普通词汇,可能达到十几万个词,解码过程将非常复杂,识别结果组合太多,识别结果不会理想。因此只有声学模型是完全不够的,需要引入语言模型来约束识别结果。让“今天天气很好”的概率高于“今天天汽很好”的概率,得到声学模型概率高,又符合表达

C语言 将“China”译成密码

将“China”译成密码,密码规律是:用原来的字母后面的第4个字母代替原来的字母。例如,字母“A”后面的第4个字母是“E”,用“E”代替“A”。因此,“China”应译为“Glmre”。编译程序用付赋初值的方法使c1,c2,c3,c4,c5这五个变量的值分别为“C”,“h”,“i”,“n”,“a”,经过运算,使c1,c2,c3,c4,c5分别变成“G”,“l”,“m”,“r”,“e”。分别用put