java - 使用 Commons Digester 解析成 HashMap

标签 java xml xquery apache-commons-digester

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

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

在 map 的第一个条目中,我想将“p1.c1”作为 map 键,而“value1”作为 map 值。如何实现这一目标?

最佳答案

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

关于java - 使用 Commons Digester 解析成 HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1513703/

相关文章:

java - java中私有(private)方法的重写

c++ - Xerces C++ XML : escape is really hard to do?

xml - Node.js:使用 "raw"格式执行 POST 请求的最佳方式?

xpath - Xquery/Xpath/* : 是什么意思

java - 我应该使用 "_activity = this;"吗?

java - 为什么我在使用 Gson.asString 时收到 java.lang.UnsupportedOperationException?

java - 如何使用jdk内置的android浏览器渲染网页?

java - 无法获取编码 XML 字符串的值

xml - 在 SQL Server 2008 中使用 XQuery 搜索内容

xml - 优化删除没有子元素的 XML 元素