【问题标题】:Parse into HashMap using Commons Digester使用 Commons Digester 解析成 HashMap
【发布时间】:2009-10-03 13:08:53
【问题描述】:

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

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

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

【问题讨论】:

    标签: java xml xquery apache-commons-digester


    【解决方案1】:

    使用 Xstream (http://x-stream.github.io/) 的示例。它不完全遵循您的 XML 规范,我将嵌套的 &lt;value&gt; 标签添加到 &lt;child&gt; 标签。

    输出:

    <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;
        }
    }
    

    【讨论】:

    • 但是......他们有没有使用消化器的解决方案?它适用于 99% 的情况。这是我们在消化器中无法处理的情况。
    【解决方案2】:

    Apache 通用摘要并不是一个真正的完整解析器,而且有时非常缓慢……如果您必须处理大型 XML 文档,您可能需要查看扩展的 VTD-XML,它支持高达 256 GB 的 XML ,它还支持内存映射,允许部分加载 XmL 文档

    【讨论】:

      【解决方案3】:

      已解决。用户扩展 hashmap 规则。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • no 100% ,我想使用 apache commons digester 来做到这一点。它适用于单一层次结构。但对于多层次对象。问题是,如何获取父键名、子键名并将它们放在一起?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多