简体   繁体   English

如何使用Java解析XML

[英]How to parse xml with java

I'm calling a soap webservice from my java application. 我正在从Java应用程序中调用Soap Web服务。 I get response and I want to parse it and get data. 我得到响应,我想解析它并获取数据。 The problem is that field <tranData> , contains structure with &gt;&lt; 问题是字段<tranData>包含&gt;&lt; instead of <> . 代替<> How can I parse this document to get data from field <tranData> ? 如何解析此文档以从<tranData>字段获取数据?

This is response structure: 这是响应结构:

<response>
<Portfolio>
    <ID>1</ID>
    <holder>2</holder>
 </Portfolio>
<tranData>     &lt;responseOne&gt;&lt;header&gt;&lt;code&gt;1&lt;/code&gt;&lt;/header&gt;&lt;/responseOne&gt;</tranData>

Please remember that, this is only a example of response, and the amount of data will be much bigger, so the solution should be fast. 请记住,这只是响应的一个示例,数据量会大很多,因此解决方案应该很快。

What you show us is the actual document as it is received over the wire, right? 您显示给我们的是通过电汇收到的实际文件,对吗? So <tranData> contains an XML string that has been escaped to not interfere with the markup of the rest of the containing document. 因此, <tranData>包含一个XML字符串,该字符串已转义为不干扰其余文档的标记。

When you read the content of the <tranData> element, the XML processor will 'unescape' the string and give you the 'original' value: 当您读取<tranData>元素的内容时,XML处理器将“转义”字符串并为您提供“原始”值:

<responseOne><header><code>1</code></header></responseOne>

What you do with that value is a different story. 您使用该价值所做的事情是一个不同的故事。 You can parse it as yet another XML document and retrieve the value of the <code> element, or just pass the string along to some other processing step. 您可以将其解析为另一个XML文档,并检索<code>元素的值,或者只是将字符串传递给其他处理步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM