如何使用DOM选择具有不同标签的节点?

时间:2015-09-20 00:30:04

标签: java xml dom domparser

我有一个xml文件,如下所示:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
 <HWData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <NE MOID="WBTS-42" NEType="WBTS">
   <EQHO MOID="EQHO-1-0" >
     <UNIT MOID="UNIT-FAN-1" State="enabled"></UNIT>
     <UNIT MOID="UNIT-FAN-3" State="enabled"></UNIT>
   </EQHO>
  </NE>
  <NE MOID="RNC-40" NEType="RNC">
   <EQHO MOID="EQHO-3-0" >
     <UNIT MOID="UNIT-FAN-5" State="disabled"></UNIT>
     <UNIT MOID="UNIT-FAN-6" State="disabled"></UNIT>
   </EQHO>
  </NE>
</HWData>

我问我怎么能得到NodeList包含&#34; NE&#34;和&#34; UNIT&#34;使用DOM的标签? 感谢

3 个答案:

答案 0 :(得分:2)

您可以手动执行此操作:

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlDomTest {
    public static void main(String[] args) throws Exception {
        File file = new File("/path/to/your/file");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder(); 
        Document doc = db.parse(file);
        Set<String> filteredNames = new HashSet<String>(Arrays.asList("NE", "UNIT"));
        NodeList list = collectNodes(doc, filteredNames);
        for (int i = 0; i < list.getLength(); i++)
            System.out.println(list.item(i).getNodeName());
    }

    private static NodeList collectNodes(Document doc, Set<String> filteredNames) {
        Node ret = doc.createElement("NodeList");
        collectNodes(doc, filteredNames, ret);
        return ret.getChildNodes();
    }

    private static void collectNodes(Node node, Set<String> filteredNames, Node ret) {
        NodeList chn = node.getChildNodes();
        for (int i = 0; i < chn.getLength(); i++) {
            Node child = chn.item(i);
            if (filteredNames.contains(child.getNodeName()))
                ret.appendChild(child);
            collectNodes(child, filteredNames, ret);
        }
    }
}

答案 1 :(得分:1)

试试这个:

public static List<String> MOIDList(File file) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException{
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder(); 
    Document doc = db.parse(file);

       XPath xPath = XPathFactory.newInstance().newXPath();
       XPathExpression exp = xPath.compile("//NE | //UNIT");
       NodeList nl = (NodeList)exp.evaluate(doc, XPathConstants.NODESET);

        List<String> MoidList = new ArrayList<>();
    for (int i = 0; i < nl.getLength(); i++) {
        String moid=((Element)nl.item(i)).getAttribute("MOID");
            MoidList.add(moid);
    }
    return MoidList;

}

答案 2 :(得分:1)

仅选择MOIDS的xpath为//NE/@MOID | //UNIT/@MOID

您应该查看我的开源Xml-parser-library unXml。它可以在Maven Central上找到。

然后您可以执行以下操作:

import com.nerdforge.unxml.Parsing;
import com.nerdforge.unxml.factory.ParsingFactory;
import org.w3c.dom.Document;
import java.util.List;

public class Parser {
    public List<String> parseXml(String xml){
        Parsing parsing = ParsingFactory.getInstance().create();
        Document document = parsing.xml().document(xml);

        List<String> result = parsing
            .arr("//NE/@MOID | //UNIT/@MOID", parsing.text())
            .as(String.class)
            .apply(document);
        return result;
    }
}

parseXml将返回结果:

[WBTS-42, UNIT-FAN-1, UNIT-FAN-3, RNC-40, UNIT-FAN-5, UNIT-FAN-6]

如果需要,您还可以创建更复杂的嵌套数据结构。如果你想要一个如何做的例子,请在这里给我一个评论。