【问题标题】:Parsing XML and populating into Map解析 XML 并填充到 Map
【发布时间】:2012-08-08 19:40:43
【问题描述】:

我有如下的xml。

<Employees>
      <Employee id="1">Chuck</Employee>
      <Employee id="2">Al</Employee>
      <Employee id="3">Kiran</Employee>
</Employees>

XML 包含大量员工。我只是为了简化而提及。

解析此 xml 并填充到地图中的最佳方法是什么?地图应该包含 id 和 name 对。

请提供代码以便更好地理解。

【问题讨论】:

  • 使用 jdom 并遍历所有添加到 Map&lt;String, Integer&gt; 的员工条目。
  • 你试过什么?您知道 SAX、DOM、XPATH 等之间的区别吗...请在发布前搜索 stackoverflow。我认为这个问题已经被回答了大约 1000 次。

标签: java xml xml-parsing


【解决方案1】:

XStream answer 似乎有很多代码。您可以使用 JDK/JRE 中的 StAX API 执行以下操作:

package forum11871952;

import java.io.FileReader;
import java.util.*;
import javax.xml.stream.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("src/forum11871952/input.xml"));
        xsr.nextTag(); // advance to Employees tag
        xsr.nextTag(); // advance to first Employer element
        Map<String,String> map = new HashMap<String,String>();
        while(xsr.getLocalName().equals("Employee")) {
            map.put(xsr.getAttributeValue("", "id"), xsr.getElementText());
            xsr.nextTag(); // advance to next Employer element
        }
    }

}

【讨论】:

    【解决方案2】:

    我们可以简单地使用 Xstream 进行映射。

    XStream xStream = new XStream(new DomDriver());
    xStream.alias("Employees", Employees.class);
    xStream.registerConverter(new MapEntryConverter());
    employeesMap = (Map<String, String>) xStream.fromXML(queryXML);
    

    创建一个将 XML 解组为 Map 对象的转换器

    private static class MapEntryConverter implements Converter {
            public boolean canConvert(Class clazz) {
                return clazz.equals(Employees.class);
            }
    
            public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
                AbstractMap<String, String> map = (AbstractMap<String, String>) value;
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    writer.startNode(entry.getKey().toString());
                    writer.setValue(entry.getValue().toString());
                    writer.endNode();
                }
            }
    
            public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
                Map<String, String> map = new HashMap<String, String>();
    
                while (reader.hasMoreChildren()) {
                    reader.moveDown();
                    map.put(reader.getAttribute(1), reader.getValue());
                    reader.moveUp();
                }
                return map;
            }
        }
    

    如下创建员工和员工类。

    private class Employees{
            List<Employee> employees;
        }
        private class Employee{
            private String id;
            private String value;
    }
    

    希望这对你有用。

    【讨论】:

      【解决方案3】:

      使用 XStream 等库。 List&lt;Employee&gt;Map 更适合这里。

      【讨论】:

      • 如果 OP 不关心查找时间,ListMap 好。
      • SetListMap 上怎么样
      • 我的要求是在地图中设置
      猜你喜欢
      • 2011-02-17
      • 2011-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      相关资源
      最近更新 更多