将XML信息提取到列表< Map>使用XPath [英] Extract XML Information to List<Map> using XPath

查看:132
本文介绍了将XML信息提取到列表< Map>使用XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自SOAP Response的XML数据,如下例所示:

I have a XML data from a SOAP Response like in the following example:

<EMP>
   <PERSONAL_DATA>
     <EMPLID>AA0001</EMPLID>
     <NAME>Adams<NAME>
   </PERSONAL_DATA>
   <PERSONAL_DATA>
     <EMPLID>AA0002<EMPLID>
     <NAME>Paul<NAME>
    </PERSONAL_DATA>
</EMP>

我想在地图中存储有关每位员工的信息(KEY,VALUE )KEY = tagname,VALUE = value
并且想要为在java中使用XPATH的所有员工创建 LIST< MAP> 。这是怎么做到的?

I want to store information about each employee in a Map(KEY,VALUE) KEY=tagname, VALUE=value and want to create a LIST<MAP> for all employees using XPATH in java. How is this done?

我尝试了以下内容:

 public static List createListMap(String path, SOAPMessage response,Map map) {
            List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();       
              try {
                 XPath xpath = XPathFactory.newInstance().newXPath();
                 XPathExpression expr = xpath.compile("//" + path + "/*");
                Object re =expr.evaluate(response.getSOAPBody(), XPathConstants.NODESET);
                NodeList nodes = (NodeList)res;                           
                for (int i = 0; i < nodes.getLength(); i++) {
                    if (nodes.item(i).getFirstChild() != null &&
                        nodes.item(i).getFirstChild().getNodeType() == 1) {
                        Map<String, Object> map1 = new HashMap<String, Object>();
                        map.put(nodes.item(i).getLocalName(),  map1);
                        createListMap(nodes.item(i).getNodeName(), response,map1);
                        list.add(map);                
                    }
                    else { 
                      map.put(nodes.item(i).getLocalName(),nodes.item(i).getTextContent());                                                  
                    }
return list;                
}

我调用了一个类似的方法createListMap(EMP ,响应,地图); (响应是SoapResponse)。
在XPATH // PERSONAL_DATA / * 中出现问题。在递归中,它列出了关于两个员工的数据,但我想将每个员工的数据存储在自己的地图中,然后创建这些MAP的列表...我该怎么做?

I called a method like createListMap("EMP",response,map); (response is a SoapResponse). The problem is coming when in XPATH //PERSONAL_DATA/*. In recursion it listed data about both of the employees, but I want to store each employee's data in its own map, then create a LIST of those MAP's... How do I do this?

推荐答案

表达式 // PERSONAL_DATA / * 选择每个 PERSONAL_DATA的所有子元素元素,导致您描述的问题。相反,选择 PERSONAL_DATA 元素并迭代他们的孩子。

The expression //PERSONAL_DATA/* selects all child elements of every PERSONAL_DATA element, causing exactly the problem you describe. Instead, select the PERSONAL_DATA elements themselves and iterate their children.

示例:

public NodeList eval(final Document doc, final String pathStr) 
        throws XPathExpressionException  {
    final XPath xpath = XPathFactory.newInstance().newXPath();
    final XPathExpression expr = xpath.compile(pathStr);
    return (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
}

public List<Map<String, String>> fromNodeList(final NodeList nodes) {
    final List<Map<String, String>> out = new ArrayList<Map<String,String>>(); 
    int len = (nodes != null) ? nodes.getLength() : 0;
    for (int i = 0; i < len; i++) {
        NodeList children = nodes.item(i).getChildNodes();
        Map<String, String> childMap = new HashMap<String, String>();
        for (int j = 0; j < children.getLength(); j++) {
            Node child = children.item(j);
            if (child.getNodeType() == Node.ELEMENT_NODE)
                childMap.put(child.getNodeName(), child.getTextContent());
        }
        out.add(childMap);
    }
    return out;
}

像这样使用:

List<Map<String, String>> nodes = fromNodeList(eval(doc, "//PERSONAL_DATA"));
System.out.println(nodes);

输出:

[{NAME=Adams, EMPLID=AA0001}, {NAME=Paul, EMPLID=AA0002}]

如果你实际上正在处理一个更复杂的结构,有额外的嵌套元素(我怀疑你是这样),那么你需要分别迭代这些层或使用像 JAXB

If you're actually dealing with a more complicated structure, with additional nested elements (which I suspect you are), then you'll need to iterate those layers separately or model your data using something like JAXB.

这篇关于将XML信息提取到列表&lt; Map&gt;使用XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆