gpt4 book ai didi

java - 使用 Java 读取 XML 中的 XML

转载 作者:行者123 更新时间:2023-12-01 14:21:03 26 4
gpt4 key购买 nike

如何在java中从XML Successful 读取此标签的值?本例中如何读取标签的数据。我收到 Theprocessing instructions target matches "[xX][mM][lL]"is not allowed.出现异常

     <?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<doServiceResponse xmlns="http://ocs.ztesoft.com">
<doServiceReturn>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;zsmart&gt;

&lt;Data&gt;
&lt;header&gt;
&lt;returnMsg&gt;Successful&lt;/returnMsg&gt;
&lt;ACTION_ID&gt;ModifyBalReturnAllBal&lt;/ACTION_ID&gt;
&lt;REQUEST_ID&gt;0032013070900000503&lt;/REQUEST_ID&gt;
&lt;returnCode&gt;0&lt;/returnCode&gt;
&lt;/header&gt;
&lt;body&gt;


&lt;TransactionSN&gt;503&lt;/TransactionSN&gt;
&lt;/body&gt;
&lt;/Data&gt;
&lt;/zsmart&gt;
</doServiceReturn></doServiceResponse></soapenv:Body></soapenv:Envelope>

JAVA代码

dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();

is = new InputSource();
is.setCharacterStream(new StringReader(respString));
doc = db.parse(is);
nodes = doc.getElementsByTagName("soapenv:Envelope");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);



NodeList txnStatus = element.getElementsByTagName("returnCode");
Element line = (Element) txnStatus.item(0);
bean.setTxnStatus(getCharacterDataFromElement(line));



NodeList message = element.getElementsByTagName("returnMsg");
line = (Element) message.item(0);
bean.setMessage(getCharacterDataFromElement(line));
}

异常

org.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed.

最佳答案

有多种方法可以将 XML 文件转换为 JAVA OBJECTSAXJAXB 算法就是其中的两种。JAXB算法更容易使用。我更喜欢使用JAXB。这里是帮助您从 XML 文件创建对象的链接。享受它...

http://www.mkyong.com/java/jaxb-hello-world-example/

关于java - 使用 Java 读取 XML 中的 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17562946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com