diff --git a/zh-cn/application-dev/faqs/faqs-ui-js.md b/zh-cn/application-dev/faqs/faqs-ui-js.md index 390b9e3753099a245a1395f9fc8e82ff7f271675..90ec607864c46ecfbeba21b7ac327ffe8c3e3ae1 100644 --- a/zh-cn/application-dev/faqs/faqs-ui-js.md +++ b/zh-cn/application-dev/faqs/faqs-ui-js.md @@ -13,25 +13,33 @@ convertxml中convert方法提供了转换xml文本为JavaScript对象的能力 ``` import convertxml from '@ohos.convertxml'; -// 代码片段 -xml = +// xml格式的字符串 +let xml = '' + '' + ' Happy' + ' Work' + ' Play' + ''; -let conv = new convertxml.ConvertXML(); +let conv = new convertxml.ConvertXML(); // 转换选项, 参考文档使用 -let options = {trim : false, declarationKey:"_declaration", - instructionKey : "_instruction", attributesKey : "_attributes", - textKey : "_text", cdataKey:"_cdata", doctypeKey : "_doctype", - commentKey : "_comment", parentKey : "_parent", typeKey : "_type", - nameKey : "_name", elementsKey : "_elements"} -let result:any = conv.convert(xml, options) // 将xml文本转为JS对象 +let options = { + trim: false, + declarationKey: "_declaration", + instructionKey: "_instruction", + attributesKey: "_attributes", + textKey: "_text", + cdataKey: "_cdata", + doctypeKey: "_doctype", + commentKey: "_comment", + parentKey: "_parent", + typeKey: "_type", + nameKey: "_name", + elementsKey: "_elements" +} +let result: any = conv.convert(xml, options) // 将xml文本转为JS对象 console.log('Test: ' + JSON.stringify(result)) -console.log('Test: ' + result._declaration._attributes.version) // xml代码片段version字段信息 -console.log('Test: ' + result._elements[0]._elements[0]._elements[0]._text) // xml代码片段title字段内容 +console.log('Test: ' + result._declaration._attributes.version) // xml字符串中version字段信息console.log('Test: ' + result._elements[0]._elements[0]._elements[0]._text) // xml字符串中title字段内容 ``` 参考文档:[xml转换JavaScript](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-convertxml.md)