繁体   English   中英

如何使用Java在hashmap中获取XML的键值

[英]How to get key-value of XML in hashmap using java

我遇到一个问题,当我获取父密钥时,无法按照XML的密钥在哈希图中获取值

我正在使用的代码:

public static void main(String[] args) throws IOException {

            try {
        String XML="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + 
                "<sj0:Msg>\r\n" + 
                "   xmlns:sj0=\"http://fakesite.org/\"\r\n" + 
                "   xmlns:sj1=\"http://fakesite.org/ind\"\r\n" + 
                "   <sj0:Hdr>\r\n" + 
                "       <sj2:Option>CreateTxn</sj2:Option>\r\n" + 
                "       <sj2:ID>172246</sj2:ID>\r\n" + 
                "       <sj2:CountryCode>CR</sj2:CountryCode>\r\n" + 
                "   </sj0:Hdr>\r\n" + 
                "   <sj0:ReqDet>\r\n" + 
                "       <sj0:MReq>\r\n" + 
                "           <sj0:qCore>\r\n" + 
                "               <sj1:Reference>12345678</sj1:Reference>\r\n" + 
                "               <sj1:BrnCode>CLM</sj1:BrnCode>\r\n" + 
                "               <sj1:Source>M1T722</sj1:Source>\r\n" + 
                "               <sj1:TxnLegCount>2</sj1:TxnLegCount>\r\n" + 
                "           </sj0:qCore>\r\n" + 
                "       </sj0:MReq>\r\n" + 
                "       <sj0:LReq>\r\n" + 
                "           <sj0:RCore>\r\n" + 
                "               <sj1:Amt>19.28</sj1:Amt>\r\n" + 
                "               <sj1:Dt>2019-09-04</sj1:Dt>\r\n" + 
                "               <sj1:Date>2019-06-27</sj1:Date>\r\n" + 
                "           </sj0:RCore>\r\n" + 
                "       </sj0:LReq>\r\n" + 
                "       <sj0:LReq>\r\n" + 
                "           <sj0:RCore>\r\n" + 
                "               <sj1:Ind>DC</sj1:Ind>\r\n" + 
                "               <sj1:Currency>US</sj1:Currency>\r\n" + 
                "               <sj1:LAmt>20.28</sj1:LAmt>\r\n" + 
                "           </sj0:RCore>\r\n" + 
                "       </sj0:LReq>\r\n" + 
                "   </sj0:ReqDet>\r\n" + 
                "</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中添加Source。 我面临遍历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;
}

暂无
暂无

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

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