thieumao

Đọc file XML

Sep 18th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.30 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package xulyxml;
  6.  
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11.  
  12. import javax.xml.parsers.DocumentBuilder;
  13. import javax.xml.parsers.DocumentBuilderFactory;
  14. import javax.xml.parsers.ParserConfigurationException;
  15. import javax.xml.xpath.XPath;
  16. import javax.xml.xpath.XPathConstants;
  17. import javax.xml.xpath.XPathExpressionException;
  18. import javax.xml.xpath.XPathFactory;
  19.  
  20. import org.w3c.dom.Document;
  21. import org.w3c.dom.Node;
  22. import org.w3c.dom.NodeList;
  23. import org.xml.sax.SAXException;
  24.  
  25. public class Main {
  26.     public static void main(String[] args) {
  27.  
  28.         try {
  29.             FileInputStream file = new FileInputStream(new File("employees.xml"));
  30.                  
  31.             DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  32.              
  33.             DocumentBuilder builder =  builderFactory.newDocumentBuilder();
  34.              
  35.             Document xmlDocument = builder.parse(file);
  36.  
  37.             XPath xPath =  XPathFactory.newInstance().newXPath();
  38.  
  39.             System.out.println("*************************");
  40.             String expression = "/Employees/Employee[@emplid='1111']/email";
  41.             System.out.println(expression);
  42.             String email = xPath.compile(expression).evaluate(xmlDocument);
  43.             System.out.println(email);
  44.  
  45.             System.out.println("*************************");
  46.             expression = "/Employees/Employee/firstname";
  47.             System.out.println(expression);
  48.             NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
  49.             for (int i = 0; i < nodeList.getLength(); i++) {
  50.                 System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
  51.             }
  52.  
  53.             System.out.println("*************************");
  54.             expression = "/Employees/Employee[@type='admin']/firstname";
  55.             System.out.println(expression);
  56.             nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
  57.             for (int i = 0; i < nodeList.getLength(); i++) {
  58.                 System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
  59.             }
  60.  
  61.             System.out.println("*************************");
  62.             expression = "/Employees/Employee[@emplid='2222']";
  63.             System.out.println(expression);
  64.             Node node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
  65.             if(null != node) {
  66.                 nodeList = node.getChildNodes();
  67.                 for (int i = 0;null!=nodeList && i < nodeList.getLength(); i++) {
  68.                     Node nod = nodeList.item(i);
  69.                     if(nod.getNodeType() == Node.ELEMENT_NODE)
  70.                         System.out.println(nodeList.item(i).getNodeName() + " : " + nod.getFirstChild().getNodeValue());
  71.                 }
  72.             }
  73.              
  74.             System.out.println("*************************");
  75.  
  76.             expression = "/Employees/Employee[age>40]/firstname";
  77.             nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
  78.             System.out.println(expression);
  79.             for (int i = 0; i < nodeList.getLength(); i++) {
  80.                 System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
  81.             }
  82.          
  83.             System.out.println("*************************");
  84.             expression = "/Employees/Employee[1]/firstname";
  85.             System.out.println(expression);
  86.             nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
  87.             for (int i = 0; i < nodeList.getLength(); i++) {
  88.                 System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
  89.             }
  90.             System.out.println("*************************");
  91.             expression = "/Employees/Employee[position() <= 2]/firstname";
  92.             System.out.println(expression);
  93.             nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
  94.             for (int i = 0; i < nodeList.getLength(); i++) {
  95.                 System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
  96.             }
  97.  
  98.             System.out.println("*************************");
  99.             expression = "/Employees/Employee[last()]/firstname";
  100.             System.out.println(expression);
  101.             nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
  102.             for (int i = 0; i < nodeList.getLength(); i++) {
  103.                 System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
  104.             }
  105.  
  106.             System.out.println("*************************");
  107.  
  108.         } catch (FileNotFoundException e) {
  109.             e.printStackTrace();
  110.         } catch (SAXException e) {
  111.             e.printStackTrace();
  112.         } catch (IOException e) {
  113.             e.printStackTrace();
  114.         } catch (ParserConfigurationException e) {
  115.             e.printStackTrace();
  116.         } catch (XPathExpressionException e) {
  117.             e.printStackTrace();
  118.         }      
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment