简体   繁体   中英

Java get XML nodes

i want to get the names of the first two Service Authors.

i have the following XML example:

<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. The xml file above was meant as an example.

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

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. Works pretty well-

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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