codecamp

xml解析与生成

说明

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

导入模块

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

XmlSerializer

XmlSerializer接口用于生成XML文件。

constructor

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

XmlSerializer的构造函数。

系统能力: SystemCapability.Utils.Lang

参数:

参数名

类型

必填

说明

buffer

ArrayBuffer | DataView

用于接收写入xml信息的ArrayBuffer或DataView内存。

encoding

string

编码格式,默认'utf-8'(目前仅支持'utf-8')。

示例:

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

setAttributes

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

设置Attributes方法。

系统能力: SystemCapability.Utils.Lang

参数:

参数名

类型

必填

说明

name

string

属性的key值。

value

string

属性的value值。

示例:

  1. const myMAX = 2048;
  2. let arrayBuffer = new ArrayBuffer(myMAX);
  3. let thatSer = new xml.XmlSerializer(arrayBuffer);
  4. thatSer.startElement("note");
  5. thatSer.setAttributes("importance1", "high1");
  6. thatSer.endElement();
  7. let result = '<note importance1="high1"/>';
  8. let view = new Uint8Array(arrayBuffer);
  9. let view1 = "";
  10. for (let i = 0; i < result.length; ++i) {
  11. view1 = view1 + String.fromCodePoint(view[i]);
  12. }
  13. console.log(view1) // <note importance1="high1"/>

addEmptyElement

addEmptyElement(name: string): void

写入一个空元素。

系统能力: SystemCapability.Utils.Lang

参数:

参数名

类型

必填

说明

name

string

该空元素的元素名。

示例:

  1. const myMAX = 2048;
  2. let arrayBuffer = new ArrayBuffer(myMAX);
  3. let thatSer = new xml.XmlSerializer(arrayBuffer);
  4. thatSer.addEmptyElement("d");
  5. let result = '<d/>';
  6. let view = new Uint8Array(arrayBuffer);
  7. let view1 = "";
  8. for (let i = 0; i < result.length; ++i) {
  9. view1 = view1 + String.fromCodePoint(view[i]);
  10. }
  11. console.log(view1) // <d/>

setDeclaration

setDeclaration(): void

设置Declaration方法。

系统能力: SystemCapability.Utils.Lang

示例:

  1. const myMAX = 2048;
  2. let arrayBuffer = new ArrayBuffer(myMAX);
  3. let thatSer = new xml.XmlSerializer(arrayBuffer);
  4. thatSer.setDeclaration();
  5. thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
  6. thatSer.startElement("note");
  7. thatSer.endElement();
  8. let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>';
  9. let view = new Uint8Array(arrayBuffer);
  10. let view1 = "";
  11. for (let i = 0; i < result.length; ++i) {
  12. view1 = view1 + String.fromCodePoint(view[i]);
  13. }
  14. console.log(view1)
  15. // <?xml version="1.0" encoding="utf-8"?>
  16. // <h:note xmlns:h="http://www.w3.org/TR/html4/"/>

startElement

startElement(name: string): void

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名

类型

必填

说明

name

string

当前元素的元素名。

示例:

  1. const myMAX = 2048;
  2. let arrayBuffer = new ArrayBuffer(myMAX);
  3. let thatSer = new xml.XmlSerializer(arrayBuffer);
  4. thatSer.setDeclaration();
  5. thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
  6. thatSer.startElement("note");
  7. thatSer.endElement();
  8. let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>';
  9. let view = new Uint8Array(arrayBuffer);
  10. let view1 = "";
  11. for (let i = 0; i < result.length; ++i) {
  12. view1 = view1 + String.fromCodePoint(view[i]);
  13. }
  14. 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

示例:

  1. const myMAX = 2048;
  2. let arrayBuffer = new ArrayBuffer(myMAX);
  3. let thatSer = new xml.XmlSerializer(arrayBuffer);
  4. thatSer.setDeclaration();
  5. thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
  6. thatSer.startElement("note");
  7. thatSer.endElement();
  8. let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>';
  9. let view = new Uint8Array(arrayBuffer);
  10. let view1 = "";
  11. for (let i = 0; i < result.length; ++i) {
  12. view1 = view1 + String.fromCodePoint(view[i]);
  13. }
  14. 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

参数:

参数名

类型

必填

说明

prefix

string

当前元素及其子元素的前缀。

namespace

string

当前元素及其子元素的命名空间。

示例:

  1. const myMAX = 2048;
  2. let arrayBuffer = new ArrayBuffer(myMAX);
  3. let thatSer = new xml.XmlSerializer(arrayBuffer);
  4. thatSer.setDeclaration();
  5. thatSer.setNamespace("h", "http://www.w3.org/TR/html4/");
  6. thatSer.startElement("note");
  7. thatSer.endElement();
  8. let result = '<?xml version="1.0" encoding="utf-8"?>\r\n<h:note xmlns:h="http://www.w3.org/TR/html4/"/>';
  9. let view = new Uint8Array(arrayBuffer);
  10. let view1 = "";
  11. for (let i = 0; i < result.length; ++i) {
  12. view1 = view1 + String.fromCodePoint(view[i]);
  13. }
  14. 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

参数:

参数名

类型

必填

说明

text

string

当前元素的注释内容。

示例:

  1. const myMAX = 2048;
  2. let arrayBuffer = new ArrayBuffer(myMAX);
  3. let thatSer = new xml.XmlSerializer(arrayBuffer);
  4. thatSer.setComment("Hello, World!");
  5. let result = '<!--Hello, World!-->';
  6. let view = new Uint8Array(arrayBuffer);
  7. let view1 = "";
  8. for (let i = 0; i < result.length; ++i) {
  9. view1 = view1 + String.fromCodePoint(view[i]);
  10. }
  11. console.log(view1) // <!--Hello, World!-->

setCDATA

setCDATA(text: string): void

写入CDATA属性。

系统能力: SystemCapability.Utils.Lang

参数:

参数名

类型

必填

说明

text

string

CDATA属性的内容。

示例:

  1. const myMAX = 2048;
  2. let arrayBuffer = new ArrayBuffer(myMAX);
  3. let thatSer = new xml.XmlSerializer(arrayBuffer);
  4. thatSer.setCDATA('root SYSTEM')
  5. let result = '<![CDATA[root SYSTEM]]>';
  6. let view = new Uint8Array(arrayBuffer);
  7. let view1 = "";
  8. for (let i = 0; i < result.length; ++i) {
  9. view1 = view1 + String.fromCodePoint(view[i]);
  10. }
  11. console.log(view1) // <![CDATA[root SYSTEM]]>

setText

setText(text: string): void

设置Text方法。

系统能力: SystemCapability.Utils.Lang

参数:

参数名

类型

必填

说明

text

string

text属性的内容。

示例:

  1. const myMAX = 2048;
  2. let arrayBuffer = new ArrayBuffer(myMAX);
  3. let thatSer = new xml.XmlSerializer(arrayBuffer);
  4. thatSer.startElement("note");
  5. thatSer.setAttributes("importance", "high");
  6. thatSer.setText("Happy1");
  7. thatSer.endElement();
  8. let result = '<note importance="high">Happy1</note>';
  9. let view = new Uint8Array(arrayBuffer);
  10. let view1 = "";
  11. for (let i = 0; i < result.length; ++i) {
  12. view1 = view1 + String.fromCodePoint(view[i]);
  13. }
  14. console.log(view1) // <note importance="high">Happy1</note>

setDocType

setDocType(text: string): void

写入DocType属性。

系统能力: SystemCapability.Utils.Lang

参数:

参数名

类型

必填

说明

text

string

DocType属性的内容。

示例:

  1. const myMAX = 2048;
  2. let arrayBuffer = new ArrayBuffer(myMAX);
  3. let thatSer = new xml.XmlSerializer(arrayBuffer);
  4. thatSer.setDocType('root SYSTEM "http://www.test.org/test.dtd"');
  5. let result = '<!DOCTYPE root SYSTEM "http://www.test.org/test.dtd">';
  6. let view = new Uint8Array(arrayBuffer);
  7. let view1 = "";
  8. for (let i = 0; i < result.length; ++i) {
  9. view1 = view1 + String.fromCodePoint(view[i]);
  10. }
  11. console.log(view1) // <!DOCTYPE root SYSTEM "http://www.test.org/test.dtd">

XmlPullParser

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

contructor

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

构造并返回一个XmlPullParser对象。

系统能力: SystemCapability.Utils.Lang

参数:

参数名

类型

必填

说明

buffer

ArrayBuffer | DataView

需要解析的xml文本信息。

encoding

string

编码格式,默认'utf-8'(目前仅支持'utf-8')。

示例:

  1. let strXml =
  2. '<?xml version="1.0" encoding="utf-8"?>' +
  3. '<!DOCTYPE note [\n<!ENTITY foo "baa">]>' +
  4. '<note importance="high" logged="true">' +
  5. ' <![CDATA[\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n]]>' +
  6. ' <!--Hello, World!-->' +
  7. ' <company>John &amp; Hans</company>' +
  8. ' <title>Happy</title>' +
  9. ' <title>Happy</title>' +
  10. ' <lens>Work</lens>' +
  11. ' <lens>Play</lens>' +
  12. ' <?go there?>' +
  13. ' <a><b/></a>' +
  14. ' <h:table xmlns:h="http://www.w3.org/TR/html4/">' +
  15. ' <h:tr>' +
  16. ' <h:td>Apples</h:td>' +
  17. ' <h:td>Bananas</h:td>' +
  18. ' </h:tr>' +
  19. ' </h:table>' +
  20. '</note>';
  21. let textEncoder = new util.TextEncoder();
  22. let arrbuffer = textEncoder.encodeInto(strXml);
  23. let that = new xml.XmlPullParser(arrbuffer.buffer, 'UTF-8');
  24. let str1 = '';
  25. function func1(name, value) {
  26. str1 += name + value;
  27. return true;
  28. }
  29. let options = { supportDoctype: true, ignoreNameSpace: true, tagValueCallbackFunction: func1 }
  30. that.parse(options);
  31. console.log(str1)
  32. // 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

用户控制以及获取解析信息的选项。

示例:

  1. let strXml =
  2. '<?xml version="1.0" encoding="utf-8"?>' +
  3. '<note importance="high" logged="true">' +
  4. ' <title>Happy</title>' +
  5. ' <todo>Work</todo>' +
  6. ' <todo>Play</todo>' +
  7. '</note>';
  8. let arrayBuffer = new ArrayBuffer(strXml.length);
  9. let bufView = new Uint8Array(arrayBuffer);
  10. let strLen = strXml.length;
  11. for (let tmp = 0; tmp < strLen; ++tmp) {
  12. bufView[tmp] = strXml.charCodeAt(tmp);
  13. }
  14. let that = new xml.XmlPullParser(arrayBuffer);
  15. let arrTag = {};
  16. let str = "";
  17. let i = 0;
  18. function func(key, value){
  19. arrTag[i] = 'key:'+key+' value:'+ value.getDepth();
  20. str += arrTag[i];
  21. i++;
  22. return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
  23. }
  24. let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
  25. that.parse(options);
  26. console.log(str);
  27. // 输出:
  28. // 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
  29. // 解析:
  30. // key代表了当前事件类型,value为当前解析的深度。你可以根据EVENTTYPE来知道具体的解析事件。例如本示例结果key: value代表含义为:
  31. // 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

名称

类型

必填

说明

supportDoctype

boolean

是否忽略Doctype , 默认false。

ignoreNameSpace

boolean

是否忽略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

返回当前列号。

示例:

  1. let strXml =
  2. '<?xml version="1.0" encoding="utf-8"?>' +
  3. '<note importance="high" logged="true">' +
  4. ' <title>Happy</title>' +
  5. ' <todo>Work</todo>' +
  6. ' <todo>Play</todo>' +
  7. '</note>';
  8. let arrayBuffer = new ArrayBuffer(strXml.length);
  9. let bufView = new Uint8Array(arrayBuffer);
  10. let strLen = strXml.length;
  11. for (let tmp = 0; tmp < strLen; ++tmp) {
  12. bufView[tmp] = strXml.charCodeAt(tmp);
  13. }
  14. let that = new xml.XmlPullParser(arrayBuffer);
  15. let arrTag = {};
  16. let str = "";
  17. let i = 0;
  18. function func(key, value){
  19. arrTag[i] = 'key:'+key+' value:'+ value.getColumnNumber();
  20. str += arrTag[i];
  21. i++;
  22. return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
  23. }
  24. let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
  25. that.parse(options);
  26. console.log(str);
  27. // 输出:
  28. // 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

返回元素的当前深度。

示例:

  1. let strXml =
  2. '<?xml version="1.0" encoding="utf-8"?>' +
  3. '<note importance="high" logged="true">' +
  4. ' <title>Happy</title>' +
  5. ' <todo>Work</todo>' +
  6. ' <todo>Play</todo>' +
  7. '</note>';
  8. let arrayBuffer = new ArrayBuffer(strXml.length);
  9. let bufView = new Uint8Array(arrayBuffer);
  10. let strLen = strXml.length;
  11. for (let tmp = 0; tmp < strLen; ++tmp) {
  12. bufView[tmp] = strXml.charCodeAt(tmp);
  13. }
  14. let that = new xml.XmlPullParser(arrayBuffer);
  15. let arrTag = {};
  16. let str = "";
  17. let i = 0;
  18. function func(key, value){
  19. arrTag[i] = 'key:'+key+' value:'+ value.getDepth();
  20. str += arrTag[i];
  21. i++;
  22. return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
  23. }
  24. let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
  25. that.parse(options);
  26. console.log(str);
  27. // 输出:
  28. // 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
  29. // 解析:
  30. // key代表了当前事件类型,value为当前解析的深度。你可以根据EVENTTYPE来知道具体的解析事件。例如本示例结果key: value代表含义为:
  31. // 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

返回当前行号。

示例:

  1. let strXml =
  2. '<?xml version="1.0" encoding="utf-8"?>' +
  3. '<note importance="high" logged="true">' +
  4. ' <title>Happy</title>' +
  5. ' <todo>Work</todo>' +
  6. ' <todo>Play</todo>' +
  7. '</note>';
  8. let arrayBuffer = new ArrayBuffer(strXml.length);
  9. let bufView = new Uint8Array(arrayBuffer);
  10. let strLen = strXml.length;
  11. for (let tmp = 0; tmp < strLen; ++tmp) {
  12. bufView[tmp] = strXml.charCodeAt(tmp);
  13. }
  14. let that = new xml.XmlPullParser(arrayBuffer);
  15. let arrTag = {};
  16. let str = "";
  17. let i = 0;
  18. function func(key, value){
  19. arrTag[i] = 'key:'+key+' value:'+ value.getLineNumber();
  20. str += arrTag[i];
  21. i++;
  22. return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
  23. }
  24. let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
  25. that.parse(options);
  26. console.log(str);
  27. // 输出:
  28. // 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

返回当前元素名称。

示例:

  1. let strXml =
  2. '<?xml version="1.0" encoding="utf-8"?>' +
  3. '<note importance="high" logged="true">' +
  4. ' <title>Happy</title>' +
  5. ' <todo>Work</todo>' +
  6. ' <todo>Play</todo>' +
  7. '</note>';
  8. let arrayBuffer = new ArrayBuffer(strXml.length);
  9. let bufView = new Uint8Array(arrayBuffer);
  10. let strLen = strXml.length;
  11. for (let tmp = 0; tmp < strLen; ++tmp) {
  12. bufView[tmp] = strXml.charCodeAt(tmp);
  13. }
  14. let that = new xml.XmlPullParser(arrayBuffer);
  15. let arrTag = {};
  16. let str = "";
  17. let i = 0;
  18. function func(key, value){
  19. arrTag[i] = 'key:'+key+' value:'+ value.getName();
  20. str += arrTag[i];
  21. i++;
  22. return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
  23. }
  24. let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
  25. that.parse(options);
  26. console.log(str);
  27. // 输出:
  28. // 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

返回当前元素的命名空间。

示例:

  1. let strXml =
  2. '<?xml version="1.0" encoding="utf-8"?>' +
  3. '<note importance="high" logged="true">' +
  4. ' <title>Happy</title>' +
  5. ' <todo>Work</todo>' +
  6. ' <todo>Play</todo>' +
  7. '</note>';
  8. let arrayBuffer = new ArrayBuffer(strXml.length);
  9. let bufView = new Uint8Array(arrayBuffer);
  10. let strLen = strXml.length;
  11. for (let tmp = 0; tmp < strLen; ++tmp) {
  12. bufView[tmp] = strXml.charCodeAt(tmp);
  13. }
  14. let that = new xml.XmlPullParser(arrayBuffer);
  15. let arrTag = {};
  16. let str = "";
  17. let i = 0;
  18. function func(key, value){
  19. arrTag[i] = 'key:'+key+' value:'+ value.getNamespace();
  20. str += arrTag[i];
  21. i++;
  22. return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
  23. }
  24. let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
  25. that.parse(options);
  26. console.log(str);
  27. // 输出:
  28. // 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

返回当前元素前缀。

示例:

  1. let strXml =
  2. '<?xml version="1.0" encoding="utf-8"?>' +
  3. '<note importance="high" logged="true">' +
  4. ' <title>Happy</title>' +
  5. ' <todo>Work</todo>' +
  6. ' <todo>Play</todo>' +
  7. '</note>';
  8. let arrayBuffer = new ArrayBuffer(strXml.length);
  9. let bufView = new Uint8Array(arrayBuffer);
  10. let strLen = strXml.length;
  11. for (let tmp = 0; tmp < strLen; ++tmp) {
  12. bufView[tmp] = strXml.charCodeAt(tmp);
  13. }
  14. let that = new xml.XmlPullParser(arrayBuffer);
  15. let arrTag = {};
  16. let str = "";
  17. let i = 0;
  18. function func(key, value){
  19. arrTag[i] = 'key:'+key+' value:'+ value.getPrefix();
  20. str += arrTag[i];
  21. i++;
  22. return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
  23. }
  24. let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
  25. that.parse(options);
  26. console.log(str);
  27. // 输出:
  28. // 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

返回当前事件的文本内容。

示例:

  1. let strXml =
  2. '<?xml version="1.0" encoding="utf-8"?>' +
  3. '<note importance="high" logged="true">' +
  4. ' <title>Happy</title>' +
  5. ' <todo>Work</todo>' +
  6. ' <todo>Play</todo>' +
  7. '</note>';
  8. let arrayBuffer = new ArrayBuffer(strXml.length);
  9. let bufView = new Uint8Array(arrayBuffer);
  10. let strLen = strXml.length;
  11. for (let tmp = 0; tmp < strLen; ++tmp) {
  12. bufView[tmp] = strXml.charCodeAt(tmp);
  13. }
  14. let that = new xml.XmlPullParser(arrayBuffer);
  15. let arrTag = {};
  16. let str = "";
  17. let i = 0;
  18. function func(key, value){
  19. arrTag[i] = 'key:'+key+' value:'+ value.getText();
  20. str += arrTag[i];
  21. i++;
  22. return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
  23. }
  24. let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
  25. that.parse(options);
  26. console.log(str);
  27. // 输出:
  28. // 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,当前元素为空元素。

示例:

  1. let strXml =
  2. '<?xml version="1.0" encoding="utf-8"?>' +
  3. '<note importance="high" logged="true">' +
  4. ' <title>Happy</title>' +
  5. ' <todo>Work</todo>' +
  6. ' <todo>Play</todo>' +
  7. '</note>';
  8. let arrayBuffer = new ArrayBuffer(strXml.length);
  9. let bufView = new Uint8Array(arrayBuffer);
  10. let strLen = strXml.length;
  11. for (let tmp = 0; tmp < strLen; ++tmp) {
  12. bufView[tmp] = strXml.charCodeAt(tmp);
  13. }
  14. let that = new xml.XmlPullParser(arrayBuffer);
  15. let arrTag = {};
  16. let str = "";
  17. let i = 0;
  18. function func(key, value){
  19. arrTag[i] = 'key:'+key+' value:'+ value.isEmptyElementTag();
  20. str += arrTag[i];
  21. i++;
  22. return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
  23. }
  24. let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
  25. that.parse(options);
  26. console.log(str);
  27. // 输出:
  28. // 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,当前文本事件仅包含空格字符。

示例:

  1. let strXml =
  2. '<?xml version="1.0" encoding="utf-8"?>' +
  3. '<note importance="high" logged="true">' +
  4. ' <title>Happy</title>' +
  5. ' <todo>Work</todo>' +
  6. ' <todo>Play</todo>' +
  7. '</note>';
  8. let arrayBuffer = new ArrayBuffer(strXml.length);
  9. let bufView = new Uint8Array(arrayBuffer);
  10. let strLen = strXml.length;
  11. for (let tmp = 0; tmp < strLen; ++tmp) {
  12. bufView[tmp] = strXml.charCodeAt(tmp);
  13. }
  14. let that = new xml.XmlPullParser(arrayBuffer);
  15. let arrTag = {};
  16. let str = "";
  17. let i = 0;
  18. function func(key, value){
  19. arrTag[i] = 'key:'+key+' value:'+ value.isWhitespace();
  20. str += arrTag[i];
  21. i++;
  22. return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
  23. }
  24. let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
  25. that.parse(options);
  26. console.log(str);
  27. // 输出:
  28. // 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

当前开始标记的属性数。

示例:

  1. let strXml =
  2. '<?xml version="1.0" encoding="utf-8"?>' +
  3. '<note importance="high" logged="true">' +
  4. ' <title>Happy</title>' +
  5. ' <todo>Work</todo>' +
  6. ' <todo>Play</todo>' +
  7. '</note>';
  8. let arrayBuffer = new ArrayBuffer(strXml.length);
  9. let bufView = new Uint8Array(arrayBuffer);
  10. let strLen = strXml.length;
  11. for (let tmp = 0; tmp < strLen; ++tmp) {
  12. bufView[tmp] = strXml.charCodeAt(tmp);
  13. }
  14. let that = new xml.XmlPullParser(arrayBuffer);
  15. let arrTag = {};
  16. let str = "";
  17. let i = 0;
  18. function func(key, value){
  19. arrTag[i] = 'key:'+key+' value:'+ value.getAttributeCount();
  20. str += arrTag[i];
  21. i++;
  22. return true; // Determines whether to continually parse, which is used to continue or terminate parsing.
  23. }
  24. let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func}
  25. that.parse(options);
  26. console.log(str);
  27. // 输出:
  28. // 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_DOCUMENT

0

启动文件事件。

END_DOCUMENT

1

结束文件事件。

START_TAG

2

启动标签事件。

END_TAG

3

结束标签事件。

TEXT

4

文本事件。

CDSECT

5

CDATA事件。

COMMENT

6

XML注释事件。

DOCDECL

7

XML文档类型声明事件。

INSTRUCTION

8

XML处理指令声明事件。

ENTITY_REFERENCE

9

实体引用事件。

WHITESPACE

10

空白事件。

启动一个Worker
框架测试的能力
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

媒体组件

像素

枚举

类型

接口

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }