Validation a DOM-Tree (Document-Instance) with a XML-Schema

F

Fabio

Hi,

I like to validate a XML-Document-Instance (below =doc).


/**********************java-Code*************************
*********************************************************
*/
DocumentBuilderFactory dbf=null;
Document doc=null;
dbf = DocumentBuilderFactory.newInstance();
DOMParser dp=new DOMParser();

DocumentBuilder db=null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e2) {
// TODO Automatisch erstellter Catch-Block
e2.printStackTrace();
}
doc=db.newDocument();
Element e=doc.createElementNS("xsi","root" );
Attr at=doc.createAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:xsi");
at.setNodeValue("http://www.w3.org/2001/XMLSchema-instance");
Attr schLoc=doc.createAttributeNS("anyXMLSchema.xsd","xsi:noNamespaceSchemaLocation");
schLoc.setNodeValue("anyXMLSchema.xsd");
Element e1=doc.createElement("child");
e1.appendChild(doc.createTextNode("12"));
e.setAttributeNodeNS(at);
e.setAttributeNodeNS(schLoc);
e.appendChild(e1);

// doc-Instance
doc.appendChild(e);
/************************JAVA-Code end*******************
*********************************************************
*/

my first problem is i can't use jdk 1.5


I found this below code from apache.org from Example DOM3.java

/***********************JAVA-Code**************************
*********************************************************
*/
..........
// set error handler on the Document
config = doc.getDomConfig(); // <-- can't use this method

config.setParameter("error-handler", errorHandler);

// set validation feature
config.setParameter("validate", Boolean.TRUE);
config.setParameter("schema-type",
"http://www.w3.org/2001/XMLSchema");
config.setParameter("schema-location","data/personal.xsd");

// remove comments from the document
config.setParameter("comments", Boolean.FALSE);

System.out.println("Normalizing document... ");
doc.normalizeDocument();
.....

/**********************JAVA-Code end*********************
*********************************************************
*/
But it not functioned

Can somebody give me a example, for validating a Doc-Instance with
a XML-Schema ???????????????

thank a lot
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top