如何使用java在哈希图中获取XML的键值



>我遇到了一个问题,当我获取父键时,我无法根据 XML 的键在我的哈希图中获取值

我正在使用的代码:

public static void main(String[] args) throws IOException {
try {
String XML="<?xml version="1.0" encoding="UTF-8"?>rn" + 
"<sj0:Msg>rn" + 
"   xmlns:sj0="http://fakesite.org/"rn" + 
"   xmlns:sj1="http://fakesite.org/ind"rn" + 
"   <sj0:Hdr>rn" + 
"       <sj2:Option>CreateTxn</sj2:Option>rn" + 
"       <sj2:ID>172246</sj2:ID>rn" + 
"       <sj2:CountryCode>CR</sj2:CountryCode>rn" + 
"   </sj0:Hdr>rn" + 
"   <sj0:ReqDet>rn" + 
"       <sj0:MReq>rn" + 
"           <sj0:qCore>rn" + 
"               <sj1:Reference>12345678</sj1:Reference>rn" + 
"               <sj1:BrnCode>CLM</sj1:BrnCode>rn" + 
"               <sj1:Source>M1T722</sj1:Source>rn" + 
"               <sj1:TxnLegCount>2</sj1:TxnLegCount>rn" + 
"           </sj0:qCore>rn" + 
"       </sj0:MReq>rn" + 
"       <sj0:LReq>rn" + 
"           <sj0:RCore>rn" + 
"               <sj1:Amt>19.28</sj1:Amt>rn" + 
"               <sj1:Dt>2019-09-04</sj1:Dt>rn" + 
"               <sj1:Date>2019-06-27</sj1:Date>rn" + 
"           </sj0:RCore>rn" + 
"       </sj0:LReq>rn" + 
"       <sj0:LReq>rn" + 
"           <sj0:RCore>rn" + 
"               <sj1:Ind>DC</sj1:Ind>rn" + 
"               <sj1:Currency>US</sj1:Currency>rn" + 
"               <sj1:LAmt>20.28</sj1:LAmt>rn" + 
"           </sj0:RCore>rn" + 
"       </sj0:LReq>rn" + 
"   </sj0:ReqDet>rn" + 
"</sj0:Msg>";

String XString = XML;
System.out.println(XML);
HashMap<String, String> values = new HashMap<String, String>();
Document xml = convertStringToDocument(XString);
Node user = xml.getFirstChild();
NodeList childs = user.getChildNodes();
Node child;
for (int i = 0; i < childs.getLength(); i++) {
child = childs.item(i);
System.out.println(child.getNodeName());
System.out.println(child.getNodeType());
System.out.println(child.getUserData("Source"));
System.out.println(child.getTextContent());
values.put(child.getNodeName(), child.getTextContent());
}
System.out.println("Source name");
System.out.println(values.toString());
System.out.println(values.get("Source"));
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
private static Document convertStringToDocument(String xmlStr) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder  = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new ByteArrayInputStream(xmlStr.getBytes("UTF-8"))));

return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

输出:

{sj0:ReqDet=

12345678
CLM
M1T722
2


19.28
2019-09-04
2019-06-27


DC
US
20.28
, #text= , sj0:Hdr=         CreateTxn       172246      CR  }

我需要在 haspmap 键而不是 ReqDet 中添加源。我遇到了遍历 XML 的问题。知道我哪里出错了,也请解释如何获取其他键值的值,即 RCore 父键的 Ind 键。

如果该库有问题,我可以使用任何其他方法或库来完成此任务

下面的代码行具有"*"这意味着它将从XML中选择所有节点/键值

NodeList nodeList = doc.getElementsByTagName("*");

以下是对我有用的完整代码:

public static void main(String[] args) throws IOException {
String XML="YOUR XML";
HashMap<String, String> values =convertStringToDocument(XML);
System.out.println("values = "+values.get("sj1:Source"));
}
public static HashMap<String, String> convertStringToDocument(String xmlStr) {
HashMap<String, String> values = new HashMap<String, String>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder  = factory.newDocumentBuilder();
EntityResolver resolver = new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) {
String empty = "";
ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
System.out.println("resolveEntity:" + publicId + "|" + systemId);
return new InputSource(bais);
}
};
builder.setEntityResolver(resolver);
Document doc = builder.parse(new InputSource(new ByteArrayInputStream(xmlStr.getBytes("UTF-8"))));
NodeList nodeList = doc.getElementsByTagName("*");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
values.put(node.getNodeName(), node.getTextContent());
}
}
return values;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}