使用 Commons Digester 解析成 HashMap

发布于 2024-08-07 04:22:15 字数 333 浏览 10 评论 0原文

我需要将 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”作为地图值。如何实现这一目标?

I need to parse xml into a HashMap where the 'key' is the concatenation of two elements attributes.
The xml looks like :

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

In the 1st entry of map, I want to put 'p1.c1'as the map key while 'value1' as the map value. How to achieve that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我早已燃尽 2024-08-14 04:22:15

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

输出:

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

An example using Xstream (http://x-stream.github.io/). It does not completely follow your XML spec, I added nested <value> tags to the <child> tags.

Output:

<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

Does this help? Otherwise please follow up.

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;
    }
}
怂人 2024-08-14 04:22:15

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

Apache common digest is not really a fully parser and it is sometimes very very slow... if you have to deal with large XML documents you probably want to check out extended VTD-XML, which supports up to 256 GB of XML, it also supports memory map, allowing partial loading of XmL document

四叶草在未来唯美盛开 2024-08-14 04:22:15

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

Resolved. user extend hashmap rule.

烟雨扶苏 2024-08-14 04:22:15

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

Do you have problem selecting and using XML parsers?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文