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>
 </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 打印带有值的节点路径。这是我必须获得的输出示例:

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代码。我无法让它正常工作。

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

请帮助我使其工作。 谢谢。

最佳答案

您遇到此问题是因为您使用 HashMap 来存储值。例如,路径 RootNode.Node_15.Node_25.Node_252 出现多次,当您将新值放入 map 时,旧值将被删除。

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

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

已编辑

路径构造也存在错误,并且变量 levelcurLevel 不是必需的。 这是一个有效的代码示例:

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节点路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11182420/

相关文章:

java - 将对象的对象绑定(bind)到表单

java - 从用户的多个 OU 递归查询 LDAP 角色

java - 使用 Lucene 6 提取英语单词

xml - 是否(实际上)有一个 1 :1 correspondence between an XML schema and an XML namespace?

xml - SQL Server XML 替换属性中的值

java - 从 Main 方法运行 intellij IDEA 插件

java - 将 3 字节数组转换为数字

javascript - jQuery 从 xml 文档中提取属性

c# - Datatable.WriteXml-如何在元素名称中保留空格

android - 如何将静态字符串与 XML 字符串资源连接起来?