0

有没有一种方法可以使用“getElementsByTagName”在 DOM 中查询两个标签名称

我试图让我的程序从两个不同的标签“学校”和“医院”解析和 XML,并显示每个标签的“id”和“text”属性

这可能吗?

我的代码是:

import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DomParse {

    public static void main(String[] args) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse("input2.xml");

    NodeList School = doc.getElementsByTagName("School");
    for(int i=0;i<School.getLength();i++){
        Node p = School.item(i);
        if(p.getNodeType()==Node.ELEMENT_NODE){
            Element person = (Element) p;
            String id = person.getAttribute("id");
            NodeList nameList = person.getChildNodes();
            for(int j=0;j<nameList.getLength();j++){
                Node n = nameList.item(j);
                if(n.getNodeType()==Node.ELEMENT_NODE){
                    Element name = (Element) n;
                    System.out.println("School "  + id + ": " + "\n" + "\n" + name.getTextContent());

                    NodeList Hospital = doc.getElementsByTagName("Hospitals");
                    for(int z=0;z<Hospital.getLength();z++){
                        Node k = Hospital.item(z);
                        if(k.getNodeType()==Node.ELEMENT_NODE){
                        Element uc = (Element) k;
                        NodeList uniqueList = uc.getChildNodes();
                        for(int l=0;l<uniqueList.getLength();l++){
                        Node a = uniqueList.item(l);
                            if(a.getNodeType()==Node.ELEMENT_NODE){
                                Element uniqcon = (Element) a;
                                System.out.println("Hospitals " + id + ": " + "\n" + "\n" + uniqcon.getTextContent() + "\n" );

                            }
                        }
                        }
                    }
                }
            }
        }
    }

    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}
}

}

输入文件是:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<school>
<Hospitals size="standard">
        <Hospital codeSet="base" included="true" analysis="nI" definedAt="scan" id="0">
            <text>New England Medical. Services only urgent procedures.</text>
            <scanPatterns>
                <scanPattern occurs="1" scanPatternId="45" definedAt="scan" id="0"/>
                <scanPattern occurs="1" scanPatternId="46" definedAt="scan" id="1"/>
            </scanPatterns>
        </Hospital>

        <Hospital codeSet="base" included="true" analysis="I" SchoolId="4" definedAt="scan" id="1">
            <text>Gibson Memorial. 20 miles away.</text>
            <scanPatterns>
                <scanPattern occurs="1" scanPatternId="42" definedAt="scan" id="0"/>
                <scanPattern occurs="1" scanPatternId="39" definedAt="scan" id="1"/>
            </scanPatterns>
        </Hospital>
        </Hospitals>
<Schools>
        <School definedAt="scan" id="0">
            <text>Craven County Middle School</text>
        </School>
        <School definedAt="scan" id="1">
            <text>WestLake Middle School</text>
        </School>
        </Schools>
</school>
4

1 回答 1

0

如果您使用 XPath,这项工作会容易得多。

XPath xPath =  XPathFactory.newInstance().newXPath();
String schoolExpression = "//School";
String hospitalExpression = "//Hospital";

使用 XPath 表达式获取 NodeList,然后遍历节点并获取 id 和文本内容:

NodeList nodeList = (NodeList) xPath.compile(schoolExpression).evaluate(xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
        System.out.println(nodeList.item(i).getAttributes().getNamedItem("id").getNodeValue());  // gets you your id
        System.out.println(nodeList.item(i).getTextContent());  // gets you the context         
}
于 2016-05-14T13:21:45.837 回答