Hi all, I am trying to get PSVI info after JAXP validation of xml instance with schema but I am not getting PSVI info. It looks like I am missing very basic thing.
I provided DOMSource and DOMResult to the JAXP validator (validate(DOMSource,DOMResult). Both these sources are created from the DocumentBuilderFactory which has attribute(org.apache.xerces.dom.PSVIDocumentImpl") . Please find the attached file if someone can pinpoint what I am missing that will great help for me. Thanks Ab
import java.io.*; import java.io.IOException; import javax.xml.XMLConstants; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Schema; import javax.xml.validation.Validator; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.Element; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import javax.xml.transform.dom.DOMSource; import org.apache.xerces.xs.*; import javax.xml.transform.dom.DOMResult; public class TestPsviInfo implements ErrorHandler { DocumentBuilderFactory dbf = null; Schema schema = null; Validator validator = null; public Document parseDom(File xmlFile, String xsdFile) { String language = XMLConstants.W3C_XML_SCHEMA_NS_URI; SchemaFactory factory = SchemaFactory.newInstance(language); try { factory.setFeature("http://apache.org/xml/features/validation/schema/augment-psvi", true); schema = factory.newSchema(new File(xsdFile)); } catch ( Exception e ) { System.out.println(e.toString()); } validator = schema.newValidator(); validator.setErrorHandler(this); Document document= null; DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); dFactory.setAttribute("http://apache.org/xml/properties/dom/document-class-name", "org.apache.xerces.dom.PSVIDocumentImpl"); dFactory.setNamespaceAware(true); DocumentBuilder dBuilder =null; try { dBuilder = dFactory.newDocumentBuilder(); } catch( Exception e) { System.out.println(e.toString()); } Document docResult = null; try { document = dBuilder.parse( new FileInputStream(xmlFile)); docResult = dBuilder.newDocument(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ElementPSVI psviElem = null; if (document.getDocumentElement().isSupported("psvi", "1.0")) { System.out.println("debug1 " ); psviElem = (ElementPSVI)document.getDocumentElement(); XSElementDeclaration decl = psviElem.getElementDeclaration(); System.out.println("debug2 " ); } DOMSource source = new DOMSource(document); DOMResult result = new DOMResult(docResult); try { validator.validate(source,result); } catch (Exception e) { System.out.println(e.toString()); } if (docResult.getDocumentElement().isSupported("psvi", "1.0")) { System.out.println("debug3 " ); psviElem = (ElementPSVI)docResult.getDocumentElement(); XSElementDeclaration decl = psviElem.getElementDeclaration(); System.out.println("debug4 " ); } return null; } public void warning(SAXParseException arg0) throws SAXException { System.out.println("instance warning " + arg0); } public void error(SAXParseException arg0) throws SAXException { System.out.println("instance error " + arg0); Node node = null; node = (Node) validator.getProperty( "http://apache.org/xml/properties/dom/current-element-node"); if (node != null) { System.out.println("node = " + node.getNodeName()); } else System.out.println("Node = null"); //e.printStackTrace(); } public void fatalError(SAXParseException arg0) throws SAXException { System.out.println("instance fatal " + arg0); } // DefaultHandler contain no-op implementations for all SAX events. // This class should override methods to capture the events of interest. public static void main(String[] args) { System.out.println("Progrma started"); File xmlFile = new File(args[0]); if (!xmlFile.exists()) { System.out.println("cannot find xml file " + args[0]); System.exit(0); } TestCurrentNode testSax = new TestCurrentNode(); testSax.parseDom(xmlFile, args[1]); System.out.println("Program ended"); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: j-users-unsubscr...@xerces.apache.org For additional commands, e-mail: j-users-h...@xerces.apache.org