使用java XML分离子节点

时间:2016-09-19 12:15:18

标签: java xml dom xpath xml-parsing

我该如何分开"学生"仅包含"电子邮件"的节点标签。下面的xml包含各种子节点,如学生标记中的名称,类,地址和电子邮件。但是,一个学生标签仅包含"电子邮件"。我应该如何仅使用Java XML单独分离该特定的Student标记。请帮助,我是Java XML的新手。

<Student>
    <name>Sample1</name>
    <class>1</class>
    <address>2525</address>
    <email>sample1@sample.com</email>
</Student>

<Student>
    <name>Sample2</name>
    <class>2</class>
    <address>2153</address>
    <email>sample2@sample.com</email>
</Student>

<Student>
    <email>sample3@sample.com</email>
</Student>

2 个答案:

答案 0 :(得分:0)

XPath表达式,仅选择不包含名称,类和地址子节点的Student节点。 '/学生[不是(姓名)而不是(班级)而不是(地址)]'。不太清楚你的约束和要求到底是什么,但是这个表达式在你的例子中会起作用。对于Java的简单代码示例,有许多好的教程,例如:https://www.tutorialspoint.com/java_xml/java_xpath_parse_document.htm

答案 1 :(得分:0)

试试这个。

    import java.io.IOException;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpression;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;

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

    public class Parser {

        private static String XPATH_EXPRESSION_STRING = "//Students/Student[not ((name) and (class)  and (address))]";
        public static void main(String[] args) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder builder;
            Document doc = null;
            try {
                builder = factory.newDocumentBuilder();
                doc = builder.parse("C:\\Users\\NPGM81B\\Desktop\\Students.xml");

                // Create XPathFactory object
                XPathFactory xpathFactory = XPathFactory.newInstance();

                // Create XPath object
                XPath xpath = xpathFactory.newXPath();
                XPathExpression expr = xpath.compile(XPATH_EXPRESSION_STRING);
                Object result = expr.evaluate(doc, XPathConstants.NODESET);
                NodeList nodes = (NodeList) result;
                for (int i = 0; i < nodes.getLength(); i++) {
                    Node node = nodes.item(i);
                    printNode(node, "********");
                }
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (XPathExpressionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        private static void printNode(Node rootNode, String spacer) {
            System.out.println(spacer + rootNode.getNodeName() + " -> " + rootNode.getNodeValue());
            NodeList nl = rootNode.getChildNodes();
            for (int i = 0; i < nl.getLength(); i++)
                printNode(nl.item(i), spacer + "   ");
        }

    }

Sample XML:
<?xml version="1.0" encoding="UTF-8"?>
<Students>
   <Student>
      <name>Sample1</name>
      <class>1</class>
      <address>2525</address>
      <email>sample1@sample.com</email>
   </Student>
   <Student>
      <name>Sample2</name>
      <class>2</class>
      <address>2153</address>
      <email>sample2@sample.com</email>
   </Student>
   <Student>
      <email>sample3@sample.com</email>
   </Student>
</Students>