2016-09-19 89 views
0

如何區分只包含「email」標籤的「學生」節點?下面的XML包含各種子節點,如學生標籤中的姓名,班級,地址和電子郵件。但是一個學生標籤只包含「電子郵件」。我應該如何使用java XML單獨分離該特定的Student標籤。請幫助,我是Java XML的新手。使用java XML分離子節點

<Student> 
    <name>Sample1</name> 
    <class>1</class> 
    <address>2525</address> 
    <email>[email protected]</email> 
</Student> 

<Student> 
    <name>Sample2</name> 
    <class>2</class> 
    <address>2153</address> 
    <email>[email protected]</email> 
</Student> 

<Student> 
    <email>[email protected]</email> 
</Student> 
+1

把所有的學生,經過所有的學生,如果一個沒有名字,沒有班級,沒有地址,那就是那個學生。 – Shadov

+0

@阿倫。我已經創建了一個示例來說明您的要求。讓我知道如果你需要更多的細節 – mhasan

+0

@mhasan。非常感謝..!!現在我得到了我真正想要的東西... – Arun

回答

0

XPath表達式僅選擇不包含名稱,類和地址子節點的Student節點。 '/學生[不(名稱)和不(名)而不是(地址)]'。不太清楚你的約束和要求是什麼,但這個表達式可以在你的例子中起作用。對於Java的簡單代碼示例,有許多很好的教程,例如:https://www.tutorialspoint.com/java_xml/java_xpath_parse_document.htm

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>[email protected]</email> 
    </Student> 
    <Student> 
     <name>Sample2</name> 
     <class>2</class> 
     <address>2153</address> 
     <email>[email protected]</email> 
    </Student> 
    <Student> 
     <email>sa[email protected]</email> 
    </Student> 
</Students>