简体   繁体   English

Java 获取 XML 节点

[英]Java get XML nodes

i want to get the names of the first two Service Authors.我想获得前两个服务作者的姓名。

i have the following XML example:我有以下 XML 示例:

<ServiceSchema:id>10</ServiceSchema:id>
<ServiceSchema:name>Service 10</ServiceSchema:name>
<ServiceSchema:authorInfos>
    <ServiceSchema:authorInfo>
        <ServiceSchema:id>1</ServiceSchema:id>
        <ServiceSchema:name>Service Author 1</ServiceSchema:name>
    <ServiceSchema:authorInfo>
    <ServiceSchema:authorInfo>
        <ServiceSchema:id>2</ServiceSchema:id>
        <ServiceSchema:name>Service Author 2</ServiceSchema:name>
    <ServiceSchema:authorInfo>
</ServiceSchema:authorInfos>

<ServiceSchema:requirements>
    <ServiceSchema:requirement>
        <ServiceSchema:id>1</ServiceSchema:id>
        <ServiceSchema:name>Requirement 1</ServiceSchema:name>
        <ServiceSchema:authorInfos>
            <ServiceSchema:authorInfo>
                <ServiceSchema:id>5</ServiceSchema:id>
                <ServiceSchema:name> Requirement Author 5</ServiceSchema:name>
            <ServiceSchema:authorInfo>
        </ServiceSchema:authorInfos>
    </ServiceSchema:requirement>
.....

I can get all name nodes with the following code:我可以使用以下代码获取所有名称节点:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource inputSource = new InputSource(new ByteArrayInputStream (xml.getBytes("utf-8")));
Document xmldocument = db.parse(inputSource);
NodeList nodeList = xmldocument.getElementsByTagNameNS("*", "name");

But i don't know how to get only the names of the outer authors.但我不知道如何只获得外部作者的名字。

Thank you.谢谢你。

Update:更新:

What I am trying to do completely is: I search different xml files for different nodes.我完全想做的是:我为不同的节点搜索不同的 xml 文件。 The xml file above was meant as an example.上面的 xml 文件只是一个例子。

For example:例如:

searchElementsXml = new String[]{"name", "id", "version", "description", "keywords", "authorInfos.authorInfo.name", "status"};

I have some fields where many values ​​can occur.我有一些字段可以出现很多值。 Like the author info.喜欢作者信息。

So i need something like:所以我需要类似的东西:

NodeList nodes = doc.getElementsByTagName("ServiceSchema:authorInfos:authorinfo:name");

is there a good solution out there?有没有好的解决方案?

如果我正确理解了您想要什么,我建议您获取所有作者信息 (ServiceSchema:authorInfo),然后获取每个 authorInfo 的名称节点。

Is your XML is proper i cant see that ServiceSchema:authorInfo tag is closing你的 XML 是否正确我看不到 ServiceSchema:authorInfo 标签正在关闭
Can you please update the output what you are looking .你能更新你正在寻找的输出吗? to get the authorsInfos you can use the below code NodeList nodes = doc.getElementsByTagName("ServiceSchema:authorInfos");要获取作者信息,您可以使用以下代码 NodeList nodes = doc.getElementsByTagName("ServiceSchema:authorInfos");

Her is the detail execution code她是详细的执行代码

File fXmlFile = new File("/Users/Downloads/test.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("ServiceSchema:authorInfos");
for (int temp = 0; temp < nodes.getLength(); temp++) {
    Node nNode = nodes.item(temp);
    System.out.println(" Current Author Element :" + nNode.getNodeName());
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        Element eElement = (Element) nNode;
        System.out.println("Author Info Name : " + eElement.getElementsByTagName("ServiceSchema:authorInfo").getLength());
    }
}

I ended up using xpath as suggested by others.我最终按照其他人的建议使用了 xpath。 Works pretty well-效果很好-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM