使用Commons Digester解析到HashMap

时间:2009-10-03 13:08:53

标签: java xml xquery apache-commons-digester

我需要将xml解析为HashMap,其中'key'是两个元素属性的串联。 xml看起来像:

<map>
  <parent key='p1'><child key='c1'> value1</child></parent>
  <parent key='p2'><child key='c2'> value1</child></parent>
</map>

在地图的第一个条目中,我想将'p1.c1'作为地图键,而'value1'作为地图值。如何实现?

4 个答案:

答案 0 :(得分:1)

Apache公共摘要实际上并不是一个完全解析器,它有时非常慢......如果你必须处理大型XML文档,你可能想要检查扩展的VTD-XML,它支持高达256 GB的XML ,它还支持内存映射,允许部分加载XmL文档

答案 1 :(得分:1)

使用Xstream(http://x-stream.github.io/)的示例。它没有完全遵循您的XML规范,我在<value>标记中添加了嵌套的<child>标记。

输出:

<map>
  <parent key="p1">
    <child key="c1">
      <value>value1</value>
    </child>
  </parent>
  <parent key="p2">
    <child key="c2">
      <value>value1</value>
    </child>
  </parent>
</map>
p1.c1=value1
p2.c2=value1

这有帮助吗?否则请跟进。

import java.util.HashMap;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class MapParser {

    public static void main(String[] args) {

        XStream xstream = new XStream(new DomDriver());
        xstream.alias("map", Map.class);
        xstream.addImplicitCollection(Map.class, "parents");
        xstream.alias("parent", Parent.class);
        xstream.useAttributeFor(Parent.class, "key");
        xstream.alias("child", Child.class);
        xstream.useAttributeFor(Child.class, "key");

        Map map = (Map) xstream
                .fromXML("<map><parent key='p1'><child key='c1'><value>value1</value></child></parent><parent key='p2'><child key='c2'><value>value1</value></child></parent></map>");

        System.out.println(xstream.toXML(map));

        java.util.Map result = new HashMap();
        for (Parent parent : map.getParents()) {

            Child child = parent.getChild();
            String key = parent.getKey() + "." + child.getKey();
            result.put(key, child.getValue());
            System.out.println(key + "=" + child.getValue());
        }
    }
}


import java.util.ArrayList;
import java.util.List;

public class Map {

    private List<Parent> parents = new ArrayList<Parent>();

    public void addParent(Parent parent) {
        parents.add(parent);
    }

    public List<Parent> getParents() {
        return this.parents;
    }
}

public class Parent {

    private String key;
    private Child child;

    public Parent(String key) {
        this.key = key;
    }

    public Child getChild() {
        return child;
    }

    public void setChild(Child child) {
        this.child = child;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }
}


public class Child {

    private String key;
    private String value;

    public Child(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

答案 2 :(得分:1)

解决。用户扩展hashmap规则。

答案 3 :(得分:0)

您在选择和使用XML解析器时遇到问题吗?