Advertisement
Ladies_Man

XML xsd xpath

May 3rd, 2016
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1.         File myxml = new File("test.xml");
  2.  
  3.         try {
  4. //dom parse
  5.             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  6.             DocumentBuilder db = dbf.newDocumentBuilder();
  7.             Document doc = db.parse(myxml);
  8.  
  9.  
  10.             Element root = doc.getDocumentElement();
  11.             System.out.println(root.getTagName());
  12.  
  13. //xpath
  14.             System.out.println("xpath:");
  15.             XPathFactory xpf = XPathFactory.newInstance();
  16.             XPath xp = xpf.newXPath();
  17.             XPathExpression xpe = xp.compile("/Address/Town[@id=456 ]/text()|" +
  18.                                              "/Address/Town[last()]/attribute::myattr");
  19.  
  20.             Object res = xpe.evaluate(doc, XPathConstants.NODESET);
  21.             NodeList nodes = (NodeList) res;
  22.             for (int i = 0; i < nodes.getLength(); i++) {
  23.                 System.out.println("node:" + nodes.item(i).getNodeName() + ": " + nodes.item(i).getNodeValue());
  24.             }
  25.  
  26. //xsd validation
  27.             SchemaFactory sf = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
  28.             File schemaLoc = new File("testschema.xsd");
  29.             Schema sc = sf.newSchema(schemaLoc);
  30.  
  31.             Validator v = sc.newValidator();
  32.             Source src = new StreamSource(myxml);
  33.  
  34.  
  35.             try {
  36.                 v.validate(src);
  37.                 System.out.println("is valid");
  38.             } catch (SAXException sax) {
  39.                 System.out.println("invalid");
  40.             }
  41.  
  42.         } catch (ParserConfigurationException e) {
  43.             e.printStackTrace();
  44.         }
  45.  
  46.  
  47.  
  48. <?xml version="1.0" encoding="utf-8"?>
  49. <Address>
  50.     <Recipient>Mr. Walter C. Brown</Recipient>
  51.     <House>49</House>
  52.     <Street>Featherstone Street</Street>
  53.     <Town id="123">LONDON</Town>
  54.     <Town id="456">MOSCOW</Town>
  55.     <Town id="789" myattr="nya">TOKYO</Town>
  56.     <PostCode>EC1Y 8SY</PostCode>
  57.     <Country>UK</Country>
  58. </Address>
  59.  
  60.  
  61.  
  62.  
  63. <?xml version="1.0" encoding="utf-8"?>
  64. <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  65.     <xs:element name="Address">
  66.         <xs:complexType>
  67.             <xs:sequence>
  68.                 <xs:element name="Recipient" type="xs:string" />
  69.                 <xs:element name="House" type="xs:string" />
  70.                 <xs:element name="Street" type="xs:string" />
  71.                 <xs:element name="Town" type="xs:string" />
  72.                 <xs:element name="County" type="xs:string" minOccurs="0" />
  73.                 <xs:element name="PostCode" type="xs:string" />
  74.                 <xs:element name="Country">
  75.                     <xs:simpleType>
  76.                         <xs:restriction base="xs:string">
  77.                             <xs:enumeration value="FR" />
  78.                             <xs:enumeration value="DE" />
  79.                             <xs:enumeration value="ES" />
  80.                             <xs:enumeration value="UK" />
  81.                             <xs:enumeration value="US" />
  82.                         </xs:restriction>
  83.                     </xs:simpleType>
  84.                 </xs:element>
  85.             </xs:sequence>
  86.         </xs:complexType>
  87.     </xs:element>
  88. </xs:schema>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement