Validate OIM XML's Using SAXParseException


  1. package com.oimacademy.DOMSource;
  2. import static org.w3c.dom.Node.ATTRIBUTE_NODE;
  3. import static org.w3c.dom.Node.CDATA_SECTION_NODE;
  4. import static org.w3c.dom.Node.COMMENT_NODE;
  5. import static org.w3c.dom.Node.DOCUMENT_TYPE_NODE;
  6. import static org.w3c.dom.Node.ELEMENT_NODE;
  7. import static org.w3c.dom.Node.ENTITY_NODE;
  8. import static org.w3c.dom.Node.ENTITY_REFERENCE_NODE;
  9. import static org.w3c.dom.Node.NOTATION_NODE;
  10. import static org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE;
  11. import static org.w3c.dom.Node.TEXT_NODE;
  12. import java.io.IOException;
  13. import java.io.StringReader;
  14. import javax.xml.parsers.DocumentBuilder;
  15. import javax.xml.parsers.DocumentBuilderFactory;
  16. import javax.xml.parsers.ParserConfigurationException;
  17. import org.w3c.dom.Document;
  18. import org.w3c.dom.DocumentType;
  19. import org.w3c.dom.Node;
  20. import org.w3c.dom.NodeList;
  21. import org.w3c.dom.Text;
  22. import org.w3c.dom.ls.DOMImplementationLS;
  23. import org.w3c.dom.ls.LSSerializer;
  24. import org.xml.sax.InputSource;
  25. import org.xml.sax.SAXException;
  26. public class ValidateXMLFromSAXParseException {
  27. public static void main(String args[]) {
  28.     DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  29.     builderFactory.setNamespaceAware(true);       // Set namespace aware
  30.     builderFactory.setValidating(true);           // and validating parser feaures
  31.     builderFactory.setIgnoringElementContentWhitespace(true); 
  32.        DocumentBuilder builder = null;
  33.     try {
  34.       builder = builderFactory.newDocumentBuilder();  // Create the parser
  35.     } catch(ParserConfigurationException e) {
  36.       e.printStackTrace();
  37.     }
  38.     Document xmlDoc = null;
  39.     try {
  40.       xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));
  41.     } catch(SAXException e) {
  42.       e.printStackTrace();
  43.     } catch(IOException e) {
  44.       e.printStackTrace();
  45.     }
  46.     DocumentType doctype = xmlDoc.getDoctype();       
  47.     if(doctype == null) {                             
  48.       System.out.println("DOCTYPE is null");
  49.     } else {                                          
  50.       System.out.println("DOCTYPE node:\n" + doctype.getInternalSubset());
  51.     }
  52.     System.out.println("\nDocument body contents are:" +xmlDoc.getDocumentElement());
  53.     DOMImplementationLS lsImpl = (DOMImplementationLS)xmlDoc.getDocumentElement().getOwnerDocument().getImplementation().getFeature("LS", "3.0");
  54.     LSSerializer serializer = lsImpl.createLSSerializer();
  55.     serializer.getDomConfig().setParameter("xml-declaration", true);
  56.     String str = serializer.writeToString(xmlDoc.getDocumentElement());
  57.     System.out.println("STRIGN XML IS ->"+str);   
  58.     listNodes(xmlDoc.getDocumentElement(),"");   
  59.   }  
  60.   static void listNodes(Node node, String indent) {
  61.     String nodeName = node.getNodeName();
  62.     System.out.println(indent+" Node: " + nodeName);
  63.     short type = node.getNodeType();
  64.     System.out.println(indent+" Node Type: " + nodeType(type));
  65.     if(type == TEXT_NODE){
  66.       System.out.println(indent+" Content is: "+((Text)node).getWholeText());
  67.     }    
  68.     NodeList list = node.getChildNodes();       
  69.     if(list.getLength() > 0) {                  
  70.       System.out.println(indent+" Child Nodes of "+nodeName+" are:");
  71.       for(int i = 0 ; i<list.getLength() ; i++) {
  72.         listNodes(list.item(i),indent+"  ");     
  73.       }
  74.     }         
  75.   }
  76.   static String nodeType(short type) {
  77.     switch(type) {
  78.       case ELEMENT_NODE:                return "Element";
  79.       case DOCUMENT_TYPE_NODE:          return "Document type";
  80.       case ENTITY_NODE:                 return "Entity";
  81.       case ENTITY_REFERENCE_NODE:       return "Entity reference";
  82.       case NOTATION_NODE:               return "Notation";
  83.       case TEXT_NODE:                   return "Text";
  84.       case COMMENT_NODE:                return "Comment";
  85.       case CDATA_SECTION_NODE:          return "CDATA Section";
  86.       case ATTRIBUTE_NODE:              return "Attribute";
  87.       case PROCESSING_INSTRUCTION_NODE: return "Attribute";
  88.     }
  89.     return "Unidentified";
  90.   }
  91.   //Sample XML file
  92.   static String xmlString ="<?xml version=\"1.0\"?>" +
  93.       "  <!DOCTYPE address" +
  94.       "  [" +
  95.       "     <!ELEMENT address (buildingnumber, street, city, state, zip)>" +
  96.       "     <!ATTLIST address xmlns CDATA #IMPLIED>" +
  97.       "     <!ELEMENT buildingnumber (#PCDATA)>" +
  98.       "     <!ELEMENT street (#PCDATA)>" +
  99.       "     <!ELEMENT city (#PCDATA)>" +
  100.       "     <!ELEMENT state (#PCDATA)>" +
  101.       "     <!ELEMENT zip (#PCDATA)>" +
  102.       "  ]>" +
  103.       "" +
  104.       "  <address>" +
  105.       "    <buildingnumber> 29 </buildingnumber>" +
  106.       "    <street> South Street</street>" +
  107.       "    <city>Vancouver</city>" +
  108.       "" +
  109.       "    <state>BC</state>" +
  110.       "    <zip>V6V 4U7</zip>" +
  111.       "  </address>";
  112. }

No comments:

Post a Comment