將XML文本轉(zhuǎn)換為JavaScript對象、以及XML文件生成和解析的一系列接口。
說明:
本模塊首批接口從API version 8開始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。
導(dǎo)入模塊
import xml from '@ohos.xml';
XmlSerializer
XmlSerializer接口用于生成XML文件。
constructor
constructor(buffer: ArrayBuffer | DataView, encoding?: string)
XmlSerializer的構(gòu)造函數(shù)。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
buffer | ArrayBuffer | DataView | 是 |
encoding | string | 否 | 編碼格式 , 默認(rèn)'utf-8'(目前僅支持'utf-8')。 |
鴻蒙開發(fā)指導(dǎo)文檔:gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
點(diǎn)擊或者復(fù)制轉(zhuǎn)到。
示例:
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
設(shè)置Attributes方法。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
name | string | 是 | 屬性的key值。 |
value | string | 是 | 屬性的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
寫入一個(gè)空元素。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
name | string | 是 | 該空元素的元素名。 |
示例:
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
編寫帶有編碼的文件聲明。
系統(tǒng)能力: 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"? >rn< 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
根據(jù)給定名稱寫入元素開始標(biāo)記。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
name | string | 是 | 當(dāng)前元素的元素名。 |
示例:
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"? >rn< 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"? >rn< h:note xmlns:h="http://www.w3.org/TR/html4/"/ >
endElement
endElement(): void
寫入元素結(jié)束標(biāo)記。
系統(tǒng)能力: 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"? >rn< 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"? >rn< h:note xmlns:h="http://www.w3.org/TR/html4/"/ >
setNamespace
setNamespace(prefix: string, namespace: string): void
寫入當(dāng)前元素標(biāo)記的命名空間。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
prefix | string | 是 | 當(dāng)前元素及其子元素的前綴。 |
namespace | string | 是 | 當(dāng)前元素及其子元素的命名空間。 |
示例:
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"? >rn< 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"? >rn< h:note xmlns:h="http://www.w3.org/TR/html4/"/ >
setComment
setComment(text: string): void
寫入comment屬性。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
text | string | 是 | 當(dāng)前元素的注釋內(nèi)容。 |
示例:
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屬性。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
text | string | 是 | CDATA屬性的內(nèi)容。 |
示例:
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]] >
[](https://gitee.com/openharmony/docs/blob/OpenHarmony-3.2-Release/zh-cn/application-dev/reference/apis/js-apis-xml.md#settext)setText
setText(text: string): void
設(shè)置Text方法。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
text | string | 是 | text屬性的內(nèi)容。 |
示例:
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屬性。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
text | string | 是 | DocType屬性的內(nèi)容。 |
示例:
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接口用于解析現(xiàn)有的XML文件。
constructor
constructor(buffer: ArrayBuffer | DataView, encoding?: string)
構(gòu)造并返回一個(gè)XmlPullParser對象。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
buffer | ArrayBuffer | DataView | 是 |
encoding | string | 否 | 編碼格式 , 默認(rèn)'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[rnfuncrion matchwo(a,6)rn{rnreturn 1;rn}rn]] >' +
' < !--Hello, World!-- >' +
' < company >John & 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。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
option | [ParseOptions] | 是 | 用戶控制以及獲取解析信息的選項(xiàng)。 |
示例:
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代表了當(dāng)前事件類型,value為當(dāng)前解析的深度。你可以根據(jù)EVENTTYPE來知道具體的解析事件。例如本示例結(jié)果key: value代表含義為:
// 0(START_DOCUMENT):0(起始深度為0), 2(START_TAG):1(解析到開始標(biāo)簽node, 對應(yīng)深度為1), 10(WHITESPACE):1(解析到空白標(biāo)簽空格, 對應(yīng)深度為1), 2(START_TAG):2(解析到開始標(biāo)簽title, 對應(yīng)深度為2), ...
ParseOptions
xml解析選項(xiàng)。
系統(tǒng)能力: 以下各項(xiàng)對應(yīng)的系統(tǒng)能力均為SystemCapability.Utils.Lang
名稱 | 類型 | 必填 | 說明 |
---|---|---|---|
supportDoctype | boolean | 否 | 是否忽略Doctype , 默認(rèn)false。 |
ignoreNameSpace | boolean | 否 | 是否忽略NameSpace,默認(rèn)false。 |
tagValueCallbackFunction | (name: string, value: string) => boolean | 否 | 獲取tagValue回調(diào)函數(shù) , 默認(rèn)null。 |
attributeValueCallbackFunction | (name: string, value: string) => boolean | 否 | 獲取attributeValue回調(diào)函數(shù) , 默認(rèn)null。 |
tokenValueCallbackFunction | (eventType: [EventType], value: [ParseInfo]) => boolean | 否 | 獲取tokenValue回調(diào)函數(shù), 默認(rèn)null。 |
ParseInfo
當(dāng)前xml解析信息。
getColumnNumber
getColumnNumber(): number
獲取當(dāng)前列號,從1開始。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
number | 返回當(dāng)前列號。 |
示例:
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
獲取元素的當(dāng)前深度。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
number | 返回元素的當(dāng)前深度。 |
示例:
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代表了當(dāng)前事件類型,value為當(dāng)前解析的深度。你可以根據(jù)EVENTTYPE來知道具體的解析事件。例如本示例結(jié)果key: value代表含義為:
// 0(START_DOCUMENT):0(起始深度為0), 2(START_TAG):1(解析到開始標(biāo)簽node, 對應(yīng)深度為1), 10(WHITESPACE):1(解析到空白標(biāo)簽空格, 對應(yīng)深度為1), 2(START_TAG):2(解析到開始標(biāo)簽title, 對應(yīng)深度為2), ...
getLineNumber
getLineNumber(): number
獲取當(dāng)前行號,從1開始。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
number | 返回當(dāng)前行號。 |
示例:
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
獲取當(dāng)前元素名稱。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
string | 返回當(dāng)前元素名稱。 |
示例:
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
獲取當(dāng)前元素的命名空間。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
string | 返回當(dāng)前元素的命名空間。 |
示例:
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
獲取當(dāng)前元素前綴。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
string | 返回當(dāng)前元素前綴。 |
示例:
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
獲取當(dāng)前事件的文本內(nèi)容。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
string | 返回當(dāng)前事件的文本內(nèi)容。 |
示例:
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
判斷當(dāng)前元素是否為空元素。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
boolean | 返回true,當(dāng)前元素為空元素。 |
示例:
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
判斷當(dāng)前文本事件是否僅包含空格字符。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
boolean | 返回true,當(dāng)前文本事件僅包含空格字符。 |
示例:
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
獲取當(dāng)前開始標(biāo)記的屬性數(shù)。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說明 |
---|---|
number | 當(dāng)前開始標(biāo)記的屬性數(shù)。 |
示例:
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
事件枚舉。
系統(tǒng)能力: 以下各項(xiàng)對應(yīng)的系統(tǒng)能力均為SystemCapability.Utils.Lang
名稱 | 值 | 說明 |
---|---|---|
START_DOCUMENT | 0 | 啟動(dòng)文件事件。 |
END_DOCUMENT | 1 | 結(jié)束文件事件。 |
START_TAG | 2 | 啟動(dòng)標(biāo)簽事件。 |
END_TAG | 3 | 結(jié)束標(biāo)簽事件。 |
TEXT | 4 | 文本事件。 |
CDSECT | 5 | CDATA事件。 |
COMMENT | 6 | XML注釋事件。 |
DOCDECL | 7 | XML文檔類型聲明事件。 |
INSTRUCTION | 8 | XML處理指令聲明事件。 |
ENTITY_REFERENCE | 9 | 實(shí)體引用事件。 |
WHITESPACE | 10 | 空白事件。 |
審核編輯 黃宇
-
接口
+關(guān)注
關(guān)注
33文章
8605瀏覽量
151194 -
XML
+關(guān)注
關(guān)注
0文章
188瀏覽量
33085 -
API
+關(guān)注
關(guān)注
2文章
1501瀏覽量
62034 -
鴻蒙
+關(guān)注
關(guān)注
57文章
2352瀏覽量
42863 -
OpenHarmony
+關(guān)注
關(guān)注
25文章
3722瀏覽量
16323
發(fā)布評論請先 登錄
相關(guān)推薦
評論