Validate OIM XML's Using SAXParseException

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.massiveGaze.DOMSource;

import static org.w3c.dom.Node.ATTRIBUTE_NODE;
import static org.w3c.dom.Node.CDATA_SECTION_NODE;
import static org.w3c.dom.Node.COMMENT_NODE;
import static org.w3c.dom.Node.DOCUMENT_TYPE_NODE;
import static org.w3c.dom.Node.ELEMENT_NODE;
import static org.w3c.dom.Node.ENTITY_NODE;
import static org.w3c.dom.Node.ENTITY_REFERENCE_NODE;
import static org.w3c.dom.Node.NOTATION_NODE;
import static org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE;
import static org.w3c.dom.Node.TEXT_NODE;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class ValidateXMLFromSAXParseException {
  public static void main(String args[]) {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);       // Set namespace aware
    builderFactory.setValidating(true);           // and validating parser feaures
    builderFactory.setIgnoringElementContentWhitespace(true); 
    
    DocumentBuilder builder = null;
    try {
      builder = builderFactory.newDocumentBuilder();  // Create the parser
    } catch(ParserConfigurationException e) {
      e.printStackTrace();
    }
    Document xmlDoc = null;

    try {
      xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));

    } catch(SAXException e) {
      e.printStackTrace();

    } catch(IOException e) {
      e.printStackTrace();
    }
    DocumentType doctype = xmlDoc.getDoctype();       
    if(doctype == null) {                             
      System.out.println("DOCTYPE is null");
    } else {                                          
      System.out.println("DOCTYPE node:\n" + doctype.getInternalSubset());
    }

    System.out.println("\nDocument body contents are:" +xmlDoc.getDocumentElement());
    DOMImplementationLS lsImpl = (DOMImplementationLS)xmlDoc.getDocumentElement().getOwnerDocument().getImplementation().getFeature("LS", "3.0");
    LSSerializer serializer = lsImpl.createLSSerializer();
    serializer.getDomConfig().setParameter("xml-declaration", true);
    String str = serializer.writeToString(xmlDoc.getDocumentElement());
    System.out.println("STRIGN XML IS ->"+str);   
    listNodes(xmlDoc.getDocumentElement(),""); 
   
   
  }
  
  
  static void listNodes(Node node, String indent) {
    String nodeName = node.getNodeName();
    System.out.println(indent+" Node: " + nodeName);
    short type = node.getNodeType();
    System.out.println(indent+" Node Type: " + nodeType(type));
    if(type == TEXT_NODE){
      System.out.println(indent+" Content is: "+((Text)node).getWholeText());
    }
    
    NodeList list = node.getChildNodes();       
    if(list.getLength() > 0) {                  
      System.out.println(indent+" Child Nodes of "+nodeName+" are:");
      for(int i = 0 ; i<list.getLength() ; i++) {
        listNodes(list.item(i),indent+"  ");     
      }
    }         
  }

  static String nodeType(short type) {
    switch(type) {
      case ELEMENT_NODE:                return "Element";
      case DOCUMENT_TYPE_NODE:          return "Document type";
      case ENTITY_NODE:                 return "Entity";
      case ENTITY_REFERENCE_NODE:       return "Entity reference";
      case NOTATION_NODE:               return "Notation";
      case TEXT_NODE:                   return "Text";
      case COMMENT_NODE:                return "Comment";
      case CDATA_SECTION_NODE:          return "CDATA Section";
      case ATTRIBUTE_NODE:              return "Attribute";
      case PROCESSING_INSTRUCTION_NODE: return "Attribute";
    }
    return "Unidentified";
  }

  //Sample XML file
  static String xmlString ="<?xml version=\"1.0\"?>" +
      "  <!DOCTYPE address" +
      "  [" +
      "     <!ELEMENT address (buildingnumber, street, city, state, zip)>" +
      "     <!ATTLIST address xmlns CDATA #IMPLIED>" +
      "     <!ELEMENT buildingnumber (#PCDATA)>" +
      "     <!ELEMENT street (#PCDATA)>" +
      "     <!ELEMENT city (#PCDATA)>" +
      "     <!ELEMENT state (#PCDATA)>" +
      "     <!ELEMENT zip (#PCDATA)>" +
      "  ]>" +
      "" +
      "  <address>" +
      "    <buildingnumber> 29 </buildingnumber>" +
      "    <street> South Street</street>" +
      "    <city>Vancouver</city>" +
      "" +
      "    <state>BC</state>" +
      "    <zip>V6V 4U7</zip>" +
      "  </address>";
}

No comments:

Post a Comment

About OIM

Oracle Identity Management enables organizations to effectively manage the end - to - end life - cycle of user ide...

Popular Posts