Java:如何获取xml节点路径

Java:如何获取xml节点路径,java,xml,Java,Xml,我有以下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> </No

我有以下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代码。我不能让它正常工作

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

    }
 }
}
请帮我把它弄好。
谢谢

您有这个问题,因为您使用了
HashMap
来存储您的值。例如,路径
RootNode.Node_15.Node_25.Node_252
多次出现,当您在映射中放置新值时,旧值将被擦除

您可以将
HashMap
替换为
列表
,以查看找到的所有路径。请参见此示例:

static List<String []> valueList = new ArrayList<String []>();
最后,您可以按如下方式显示路径:

Iterator<String []> it = valueList.iterator();
for (;it.hasNext();) {
    String [] val = it.next();
    System.out.println(val[0] + " = " + val[1] );
}

请解释为什么你的代码不能正常工作。它有什么问题,使我们更容易。它产生错误的输出。谢谢你的回答。现在它显示所有分支,但路径仍然不正确:RootNode.Node_11=级别_1-Value_1 RootNode.Node_12=级别_1-Value_2 RootNode.Node_13.Node_121=级别_2-Value_1 RootNode.Node_14.Node_12341=级别_4-Value_1 RootNode.Node_14.Node_121.Node_1231=级别_4-Value_2 RootNode.NodeValue_251 RootNode.Node_15.Node_25=Value_252 RootNode.Node_15.Node_251=Value_253 RootNode.Node_15.Node_251=Value_255 RootNode.Node_15=Value_256 RootNode.Node_251=Value_257 RootNode.Node_15=Value_258;您有一个带有
tagPath+=““+listOfNodes.item(i).getNodeName()的bug和变量
level
curlevel
不是必需的。请看我编辑的答案中的代码示例。超级查福因,你能再帮我一次吗。我不能在这里使用递归。有没有方法不用递归就可以做到这一点?谢谢。我已经做了一个没有递归的例子,你可以找到它。基本上,我们有要处理的节点列表。我们得到列表的第一个节点,并将其子节点添加到列表中。我们还构建了一个映射,将每个节点与其在XML中的路径相关联。
static List<String []> valueList = new ArrayList<String []>();
valueList.add(new String [] {tagPath, value});
Iterator<String []> it = valueList.iterator();
for (;it.hasNext();) {
    String [] val = it.next();
    System.out.println(val[0] + " = " + val[1] );
}
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 });
        }
    }
}