[英]XML parsing using java
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body><getSessionId xmlns="http://treecorp.corp:8080/MEENH/service">
<multiRef xmlns:apachesoap="http://xml.apache.org/xml-soap"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xsi:type="apachesoap:Map">
<item>
<key xsi:type="soapenc:string">Application</key>
<value xsi:type="soapenc:string">MobileDevice</value>
</item>
<item>
<key xsi:type="soapenc:string">Version</key>
<value xsi:type="soapenc:string">1.0</value>
</item>
<item>
<key xsi:type="soapenc:string">Username</key>
<value xsi:type="soapenc:string">ramau</value>
</item>
<item>
<key xsi:type="soapenc:string">token</key>
<value xsi:type="soapenc:string"></value>
</item>
<item>
<key xsi:type="soapenc:string">sessionID</key>
<value xsi:type="soapenc:string">SESSIONID</value>
</item>
<item>
<key xsi:type="soapenc:string">OSInformation</key>
<value xsi:type="soapenc:string">windowsXP</value>
</item>
</multiRef>
</getSessionId>
</soap:Body>
</soap:Envelope>
这是我的XML。
我想使用Java解析这个XML并根据密钥检索一个值。 您可以在XML中看到有很多关键值对。
例如,如果我想要检索应用程序的值,我应该将值作为MobileDevice获取。 像这样,任何人都帮我解决这个问题。
提前致谢。
您可以使用DSM流解析库将键值对转换为List,然后获得所需的内容。
这是第一种方法。 为此,您应该按如下方式定义yaml映射文件。
result:
type: array # result is array
path: /.+item # read item tag. the path is a regex.
fields:
key:
value:
用于解析XML的Java代码:
DSM dsm=new DSMBuilder(new File("path/to/mapping.yaml")).setType(DSMBuilder.TYPE.XML).create();
List<Map<String, Object>> itemList = (List<Map<String, Object>>)dsm.toObject(xmlFileContent);
这是转换为json的itemList列表。
[ {
"key" : "Application",
"value" : "MobileDevice"
}, {
"key" : "Version",
"value" : "1.0"
}, {
"key" : "Username",
"value" : "ramau"
}, {
"key" : "token",
"value" : null
}, {
"key" : "sessionID",
"value" : "SESSIONID"
}, {
"key" : "OSInformation",
"value" : "windowsXP"
} ]
第二种方法是将XML转换为java映射 ,它映射的关键是key标记的 值 ,map的值是 XML中value标记的值 。
这是映射文件:
result:
type: object # result is map
path: /.+multiRef # path is regex
fields:
key:
path: item/key # read value of /.+multiRef/item/key tag
value:
path: item/value # read value of /.+multiRef/item/value tag
application:
parentPath: item # assign default value when /.+multiRef/item tag is closed
default: $self.data.value # get value of value field.
filter: $self.data.key=='Application' # assign if key filed is 'Application'
version:
parentPath: item
default: $self.data.value
filter: $self.data.key=='Version'
username:
parentPath: item
default: $self.data.value
filter: $self.data.key=='Username'
token:
parentPath: item
default: $self.data.value
filter: $self.data.key=='token'
sessionID:
parentPath: item
default: $self.data.value
filter: $self.data.key=='sessionID'
OSInformation:
parentPath: item
default: $self.data.value
filter: $self.data.key=='OSInformation'
用于解析XML的Java代码:
DSM dsm=new DSMBuilder(new File("path/to/mapping.yaml")).setType(DSMBuilder.TYPE.XML).create();
Map<String, Object> result = (Map<String, Object>)dsm.toObject(xmlFileContent);
String application=result.get("application").toString()
这是Result的JSON表示:
{
"key" : "OSInformation",
"value" : "windowsXP",
"application" : "MobileDevice",
"version" : "1.0",
"username" : "ramau",
"token" : null,
"sessionID" : "SESSIONID",
"OSInformation" : "windowsXP"
}
首先,它有点复杂,但如果你看一下docs就很容易了。
使用DOM,如下所示: http : //www.java-samples.com/showtutorial.php?tutorialID = 152。
由于它是一个soap响应,为什么不编写一个很好的简单Web服务客户端来检索响应并自动将其转换为对象。
有这样一个例子在这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.