Java:如何获取xml节点路径 [英] Java: How to get xml nodes path
问题描述
我有以下 xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<RootNode>
<Node_11>LEVEL_1-Value_1</Node_11>
<Node_12>LEVEL_1-Value_2</Node_12>
<Node_13>
<Node_121>LEVEL_2-Value_1</Node_121>
</Node_13>
<Node_14>
<Node_121>
<Node_1231>
<Node_12341>LEVEL_4-Value_1</Node_12341>
<Node_12342>LEVEL_4-Value_2</Node_12342>
<Node_12343>LEVEL_4-Value_3</Node_12343>
</Node_1231>
</Node_121>
</Node_14>
<Node_15>
<Node_25>
<Node_251>Value_251</Node_251>
<Node_252>Value_252</Node_252>
</Node_25>
<Node_25>
<Node_251>Value_253</Node_251>
<Node_252>Value_254</Node_252>
</Node_25>
<Node_25>
<Node_251>Value_255</Node_251>
<Node_252>Value_256</Node_252>
</Node_25>
<Node_25>
<Node_251>Value_257</Node_251>
<Node_252>Value_258</Node_252>
</Node_25>
</Node_15>
</RootNode>
我必须使用 java 打印带值的节点路径.这是我必须获得的输出示例:
I have to print nodes path with value using java. Here is the sample of output I have to get:
RootNode.Node_11 = LEVEL_1-Value_1
RootNode.Node_12 = LEVEL_1-Value_2
RootNode.Node_13.Node_121 = LEVEL_2-Value_1
RootNode.Node_14.Node_121.Node_1231.Node_12341 = LEVEL_4-Value_1
RootNode.Node_14.Node_121.Node_1231.Node_12342 = LEVEL_4-Value_2
RootNode.Node_14.Node_121.Node_1231.Node_12343 = LEVEL_4-Value_3
RootNode.Node_15.Node_25.Node_251 = Value_251
RootNode.Node_15.Node_25.Node_252 = Value_252
RootNode.Node_15.Node_25.Node_251 = Value_253
RootNode.Node_15.Node_25.Node_252 = Value_254
RootNode.Node_15.Node_25.Node_251 = Value_255
RootNode.Node_15.Node_25.Node_252 = Value_256
RootNode.Node_15.Node_25.Node_251 = Value_257
RootNode.Node_15.Node_25.Node_252 = Value_258
这是我最后的 Java 代码.我不能让它正常工作.
Here is my last java code. I cannot get it work correctly.
public class Read_XML {
static String rootTag = "";
static HashMap valueMap = new HashMap();
public static void main(String arg[]) throws IOException, AxiomRuntimeException
{
File inFile = new File("C:/Test.xml");
FileReader fr = new FileReader(inFile);
BufferedReader br = new BufferedReader(fr);
String sXML = "";
for (String line; (line = br.readLine())!= null;)
{
sXML += line;
}
Document doc = XMLStringParser.parseString(sXML);
Element root = doc.getDocumentElement();
rootTag += root.getTagName();
NodeList rootList = doc.getElementsByTagName("RootNode");
Element rootList_node = (Element) rootList.item(0);
NodeList kids = rootList_node.getChildNodes();
for(int i = 0; i < kids.getLength(); i++)
{
String nextTag = "";
if(kids.item(i) instanceof Element)
{
nextTag = rootTag + "." + kids.item(i).getNodeName();
int level = 0;
processChildNode(kids.item(i).getChildNodes(), nextTag, level);
}
}
Iterator it = valueMap.keySet().iterator();
for (;it.hasNext();)
{
String key = it.next().toString();
String val = valueMap.get(key).toString();
System.out.println(key + " = " + val );
}
}
public static void processChildNode(NodeList listOfNodes, String tags, int level)
{
String tagPath = tags;
int curLevel = 0;
for(int i = 0; i < listOfNodes.getLength(); i++)
{
System.out.println("Node Name = " + listOfNodes.item(i).getNodeName());
if(listOfNodes.item(i) instanceof Element)
{
if(curLevel == level + 1)
{
tagPath = tags;
}
else
{
curLevel = level +1;
tagPath += "." + listOfNodes.item(i).getNodeName();
}
if(listOfNodes.item(i).getChildNodes().getLength() >= 1)
{
processChildNode(listOfNodes.item(i).getChildNodes(), tagPath, curLevel);
}
}
else if(listOfNodes.item(i) instanceof Text && listOfNodes.getLength() == 1)
{
String value = listOfNodes.item(i).getNodeValue();
valueMap.put(tagPath, value);
}
}
}
}
这是当前的输出:
RootNode.Node_15.Node_25.Node_251 = Value_251
RootNode.Node_14.Node_121.Node_1231.Node_12341 = LEVEL_4-Value_1
RootNode.Node_12 = LEVEL_1-Value_2
RootNode.Node_15 = Value_258
RootNode.Node_14.Node_121.Node_1231 = LEVEL_4-Value_3
RootNode.Node_15.Node_25 = Value_252
RootNode.Node_11 = LEVEL_1-Value_1
RootNode.Node_15.Node_251 = Value_257
RootNode.Node_13.Node_121 = LEVEL_2-Value_1
请帮助我让它工作.谢谢.
Please help me to make it work. Thanks.
推荐答案
您遇到此问题是因为您使用 HashMap
来存储您的值.例如路径 RootNode.Node_15.Node_25.Node_252
出现多次,当你在 Map 中放入一个新值时,旧值会被删除.
You have this problem because you use a HashMap
for storing your values. As for example the path RootNode.Node_15.Node_25.Node_252
is present several times, the old value is erased when you put a new one in the Map.
您可以将 HashMap
替换为 List
以查看所有找到的路径.看这个例子:
You can replace your HashMap
with a List
in order to see all the found paths. See this example :
static List<String []> valueList = new ArrayList<String []>();
然后您以这种方式添加值:
and you add the values this way :
valueList.add(new String [] {tagPath, value});
最后,您可以按如下方式显示您的路径:
Finally, you can display your paths as follows :
Iterator<String []> it = valueList.iterator();
for (;it.hasNext();) {
String [] val = it.next();
System.out.println(val[0] + " = " + val[1] );
}
已编辑
路径构建也有一个bug,变量level
和curLevel
不是必须的.这是一个有效的代码示例:
There is also a bug with path construction, and the variables level
and curLevel
are not necessary.
Here is a code example that works :
public static void processChildNode(NodeList listOfNodes, String tags) {
for (int i = 0; i < listOfNodes.getLength(); i++) {
if (listOfNodes.item(i) instanceof Element) {
String tagPath = tags + "." + listOfNodes.item(i).getNodeName();
if (listOfNodes.item(i).getChildNodes().getLength() >= 1) {
processChildNode(listOfNodes.item(i).getChildNodes(),
tagPath);
}
} else if (listOfNodes.item(i) instanceof Text
&& listOfNodes.getLength() == 1) {
String value = listOfNodes.item(i).getNodeValue();
valueList.add(new String[] { tags, value });
}
}
}
这篇关于Java:如何获取xml节点路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!