Java从XML和XPath获取JSON
如果我传递XML文档和XPath,有没有API可以为我提供字符串格式的JSON?如果我传递xpath和XML文档,如下所示
/domain:networkConfig/devices[@type='computer']/device/ipDetails/ipDetail
XML是
<?xml version="1.0" encoding="UTF-8"?>
<domain:networkConfig >
<devices type="computer">
<device vs:registerSysid="897">
<ipDetails>
<ipDetail nic="eth0" nicDesc="myZone" primaryEditable="false">
<ipAddress>192.168.31.101</ipAddress>
<gateway>192.168.31.31</gateway>
<netmask>255.255.255.0</netmask>
</ipDetail>
</ipDetails>
<routeDetails/>
</device>
</devices>
</domain:networkConfig>
然后它应该将JSON作为字符串提供给我,如下所示
"ipDetail": {
"nic": "eth0",
"nicDesc": "myZone",
"primaryEditable": false
}
# 1 楼答案
xslt 3.0提供了一个xml-to-json()函数,该函数在Saxon 9.6中可用。在将XML转换为JSON之前,您必须将其转换为正确的格式,但是在XSLT中转换XML是该语言擅长的