Java 使用Commons Digester解析为HashMap
我需要将xml解析为HashMap,其中“键”是两个元素属性的串联。 xml看起来像:Java 使用Commons Digester解析为HashMap,java,xml,xquery,apache-commons-digester,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> 价值1 价值1 在m
<map>
<parent key='p1'><child key='c1'> value1</child></parent>
<parent key='p2'><child key='c2'> value1</child></parent>
</map>
价值1
价值1
在map的第一个条目中,我想把'p1.c1'作为map键,而'value1'作为map值。如何实现这一点?您在选择和使用XML解析器时有问题吗?Apache common digest不是一个真正完整的解析器,有时速度非常慢。。。如果您必须处理大型XML文档,您可能希望查看扩展VTD-XML,它支持多达256 GB的XML,它还支持内存映射,允许部分加载XML文档,例如使用Xstream()。它不完全符合您的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
价值1
价值1
p1.c1=值1
p2.c2=值1
这有用吗?否则请跟进
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;
}
}
import java.util.HashMap;
导入com.thoughtworks.xstream.xstream;
导入com.thoughtworks.xstream.io.xml.DomDriver;
公共类映射分析器{
公共静态void main(字符串[]args){
XStream XStream=newxstream(newdomdriver());
别名(“map”,map.class);
addImplicitCollection(Map.class,“父级”);
别名(“parent”,parent.class);
useAttributeFor(Parent.class,“key”);
别名(“child”,child.class);
useAttributeFor(Child.class,“key”);
Map Map=(Map)xstream
.fromXML(“value1value1”);
System.out.println(xstream.toXML(map));
java.util.Map result=new HashMap();
for(父级:map.getParents()){
Child=parent.getChild();
字符串key=parent.getKey()+“+child.getKey();
put(key,child.getValue());
System.out.println(key+“=”+child.getValue());
}
}
}
导入java.util.ArrayList;
导入java.util.List;
公共类地图{
private List parents=new ArrayList();
公共void addParent(父级){
parents.add(parent);
}
公共列表getParents(){
把这个还给父母;
}
}
公共类父类{
私钥;
独生子女;
公共父项(字符串键){
this.key=key;
}
公共子getChild(){
返回儿童;
}
公共无效集合子对象(子对象){
这个孩子=孩子;
}
公共字符串getKey(){
返回键;
}
公共无效设置键(字符串键){
this.key=key;
}
}
公营儿童{
私钥;
私有字符串值;
公共子项(字符串键){
this.key=key;
}
公共字符串getKey(){
返回键;
}
公共无效设置键(字符串键){
this.key=key;
}
公共字符串getValue(){
返回值;
}
公共void设置值(字符串值){
这个值=值;
}
}
已解决。用户扩展hashmap规则。no 100%,我想使用apache commons digester来实现这一点。它适用于单一层次结构。但对于多层次对象。问题是,如何获取父密钥名、子密钥名并将它们组合在一起?但是。。。他们在消化池里用过什么溶液吗?它适用于99%的情况。这是我们在消化池内无法处理的情况。