xml解析库能力,但是xml转json的也存在问题,如何解决。

@ohos.xml官方提供的xml 解析库,官方示例无法准确解析每一个元素或者标签值,数据是复杂的xml结构,必须使用xml 解析能力或者xml转json的能力。但是xml转json的也存在问题,如何解决。


HarmonyOS
2024-05-30 23:06:12
3175浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
netos

可以通过ParseOptions接口里的函数能力来实现解析能力。

示例代码:

import xml from '@ohos.xml'; 
import util from '@ohos.util'; 
 
let strXml: string = 
  '<?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 & Hans</company>' + 
    ' <title>Happy</title>' + 
    ' <title class=\'a\'>Happy11</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: util.TextEncoder = new util.TextEncoder(); 
let arrBuffer: Uint8Array = textEncoder.encodeInto(strXml); 
let that: xml.XmlPullParser = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8'); 
let str: string = ''; 
 
function tagFunc(name: string, value: string): boolean { 
  str = name + value; 
  console.info('tag-' + str); 
  return true; 
} 
 
function attFunc(name: string, value: string): boolean { 
  str = name + ' ' + value; 
  console.info('attri-' + str); 
  return true; 
} 
 
function tokenFunc(name: xml.EventType, value: xml.ParseInfo): boolean { 
  str = name + ' ' + value.getDepth(); 
  console.info('token-' + str); 
  return true; 
} 
 
let options: xml.ParseOptions = { 
  supportDoctype: true, 
  ignoreNameSpace: true, 
  tagValueCallbackFunction: tagFunc, 
  attributeValueCallbackFunction: attFunc, 
  tokenValueCallbackFunction: tokenFunc 
}; 
that.parse(options); 
 
@Entry 
@Component 
struct Index11 { 
  @State message: string = 'Hello World'; 
 
  aboutToAppear() { 
    that.parse(options) 
  } 
 
  build() { 
    Row() { 
      Text() { 
        Span('来自QQQq') 
      } 
    } 
    .width(140) 
    .height(100) 
    .backgroundColor(Color.Yellow) 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.

文档链接:

XML解析

分享
微博
QQ
微信
回复
2024-05-31 22:20:03


相关问题
HarmonyOS 如何解析xml文件
471浏览 • 1回复 待解决
HarmonyOS xml解析工具
422浏览 • 1回复 待解决
HarmonyOS xml解析失败
318浏览 • 1回复 待解决
HarmonyOS xml解析异常
458浏览 • 1回复 待解决
怎样实现XML标签和标签值解析
688浏览 • 1回复 待解决
xml文件数据解析 ,都有哪些步骤?
1236浏览 • 1回复 待解决
关于xml中include使用问题
7702浏览 • 2回复 待解决
如何解决底层无法调试问题
1448浏览 • 1回复 待解决
鸿蒙 | Jar包中解析xml文件为空值。
8002浏览 • 1回复 待解决