gpt4 book ai didi

java - DOM 解析器读取 Xml、检索属性值并存储它们

转载 作者:太空宇宙 更新时间:2023-11-04 06:57:43 24 4
gpt4 key购买 nike

使用 DOM 解析器,我将属性值存储在 Java 的 HashMap 中,然后根据需要检索它。有没有其他方法可以在不使用 map 的情况下做到这一点?

Xml示例:

<?xml version="1.0" encoding="UTF-8"?>
<Main>
<SubMainLevels>
<SubMainLevel LineNo="1" arg2="122" RollNo="11" weight="14" folds="1" />
<SubMainLevel LineNo="2" arg2="123" RollNo="12" weight="12" folds="2" />
</SubMainLevels>
</Main>

我做了什么:

   Map<String, String> x = new HashMap<String, String>(); 
getXmlAttr(){
Traverse till Main\SubMainLevels\SubMainLevel

for each SubMainLevel {
getAttribute() for all attributes LineNo,arg2,RollNo,weight,folds
concatenate strConcat = arg2+":"+RollNo+":"+weight+":"+folds
x.put(LineNo,strConcat);
}
}




retrieveAttrMap(HashMap x){
for each HashMap key{
strVal =x.get(key)
split strVal at ":"
//do stuff
}
}

最佳答案

您可以使用POJO类来存储属性,而不仅仅是使用字符串。

该程序使用 BeanUtils 将属性填充到 bean 中。

这是代码

import java.io.FileInputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.beanutils.BeanUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class DOMParsarDemo2 {
protected DocumentBuilder docBuilder;
protected Element root;

private static List<SubMainLevel> subMainLevels = new ArrayList<SubMainLevel>();

public DOMParsarDemo2() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
docBuilder = dbf.newDocumentBuilder();
}

public void parse(String file) throws Exception {
Document doc = docBuilder.parse(new FileInputStream(file));
root = doc.getDocumentElement();
System.out.println("root element is :" + root.getNodeName());
}

public void printAllElements() throws Exception {
printElement(root);
}

public void printElement(Node node) {
if (node.getNodeType() != Node.TEXT_NODE) {
Node child = node.getFirstChild();
while (child != null) {
if ("SubMainLevel".equals(child.getNodeName())) {
NamedNodeMap namedNodeMap = child.getAttributes();

Map<String, String> attrMap = new HashMap<String, String>();
for (int i = 0; i < namedNodeMap.getLength(); i++) {
Node n = namedNodeMap.item(i);
attrMap.put(n.getNodeName(), n.getNodeValue());
}

SubMainLevel subMainLevel = new SubMainLevel();
try {
BeanUtils.populate(subMainLevel, attrMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
subMainLevels.add(subMainLevel);
}
printElement(child);
child = child.getNextSibling();
}
}
}

public static void main(String args[]) throws Exception {
DOMParsarDemo2 demo = new DOMParsarDemo2();
demo.parse("resources/abc1.xml");
demo.printAllElements();

for (SubMainLevel subMainLevel : subMainLevels) {
System.out.println(subMainLevel);
}
}
}

POJO:

import java.io.Serializable;    

public class SubMainLevel implements Serializable {

private static final long serialVersionUID = 1L;

public SubMainLevel() {

}

private String LineNo;
private String arg2;
private String RollNo;
private String weight;
private String folds;

public String getLineNo() {
return LineNo;
}

public void setLineNo(String lineNo) {
LineNo = lineNo;
}

public String getArg2() {
return arg2;
}

public void setArg2(String arg2) {
this.arg2 = arg2;
}

public String getRollNo() {
return RollNo;
}

public void setRollNo(String rollNo) {
RollNo = rollNo;
}

public String getWeight() {
return weight;
}

public void setWeight(String weight) {
this.weight = weight;
}

public String getFolds() {
return folds;
}

public void setFolds(String folds) {
this.folds = folds;
}

@Override
public String toString() {
return "LineNo=" + getLineNo() + " arg2=" + getArg2() + " RollNo=" + getRollNo()
+ " weight=" + getWeight() + " folds=" + getFolds();
}

}

关于java - DOM 解析器读取 Xml、检索属性值并存储它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22497563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com