problem while creating xml using jdk1.6

Hi All,

I am creating an xml file and validating with schema, problem is that code is running fine with jdk1.5 but giving problem with jdk1.6(error).

Please have a glance on the following code to solve problem.

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Poem'.

at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)

at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)

at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)

at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)

at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1887)Validation UnSuccesful

at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:685)

at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.beginNode(DOMValidatorHelper.java:273)

at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:240)

at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:186)

at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:100)

at javax.xml.validation.Validator.validate(Validator.java:127)

at src.DocWriteDOM.validateXmlDocument(DocWriteDOM.java:127)

at src.DocWriteDOM.makeDoc(DocWriteDOM.java:101)

at src.DocWriteDOM.main(DocWriteDOM.java:41)

////////////////////////////////////////////////////////////////////////

java code

public class DocWriteDOM {

public static void main(String[] av) throws IOException {

FileOutputStream os = null;

PrintStream ps = null;

try{

DocWriteDOM dw = new DocWriteDOM();

Document doc = dw.makeDoc();

TransformerFactory transfac = TransformerFactory.newInstance();

Transformer trans = transfac.newTransformer();

trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

trans.setOutputProperty(OutputKeys.INDENT, "yes");

//create string from xml tree

StringWriter sw = new StringWriter();

StreamResult result = new StreamResult(sw);

DOMSource source = new DOMSource(doc);

trans.transform(source, result);

String xmlString = sw.toString();

//print xml

// System.out.println("Here's the xml:\n\n" + xmlString);

os = new FileOutputStream(new File("Demo1.xml"));

ps = new PrintStream(os);

ps.print(xmlString);

// dw.testValidator();

}catch(Exception e){

e.printStackTrace();

}

}

/** Generate the XML document */

protected Document makeDoc() {

try {

DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();

DocumentBuilder parser = fact.newDocumentBuilder();

Document doc = parser.newDocument();

Node root = doc.createElement("Poem");

doc.appendChild(root);

Node stanza = doc.createElement("Stanza");

root.appendChild(stanza);

Node line = doc.createElement("Line");

stanza.appendChild(line);

line.appendChild(doc.createTextNode("Once, upon a midnight dreary"));

line = doc.createElement("Line");

stanza.appendChild(line);

line.appendChild(doc.createTextNode("While I pondered, weak and weary"));

this.validateXmlDocument(doc);

//System.out.println()

return doc;

} catch (Exception ex) {

System.err.println("+============================+");

System.err.println("|XML Error|");

System.err.println("+============================+");

System.err.println(ex.getClass());

System.err.println(ex.getMessage());

System.err.println("+============================+");

return null;

}

}

private boolean validateXmlDocument(Document document){

boolean isXmlDocumentValid = false;

try{

SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

// load a WXS schema, represented by a Schema instance

Source schemaFile = new StreamSource(new File("Demo1.xsd"));

Schema schema = factory.newSchema(schemaFile);

// create a Validator instance, which can be used to validate an instance document

Validator validator = schema.newValidator();

// validate the DOM tree

validator.validate(new DOMSource(document));

System.out.println("Validation successful");

isXmlDocumentValid = true;

return isXmlDocumentValid;

}catch (SAXException e) {

// instance document is invalid!

e.printStackTrace();

System.out.println("Validation UnSuccesful");

}finally{

return isXmlDocumentValid;

}

}//end of method

}

///////////////////////////////////////////////////////////////////////////////////////////////////////

Demo1.xml

<Poem>

<Stanza>

<Line>Once, upon a midnight dreary</Line>

<Line>While I pondered, weak and weary</Line>

</Stanza>

</Poem>

//////////////////////////////////////////////////////////////////

Demo1.xsd

<?xml version="1.0" encoding="ISO-8859-1" ?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Poem">

<xs:complexType>

<xs:sequence>

<xs:element name="Stanza" minOccurs="0">

<xs:complexType>

<xs:sequence>

<xs:element name="Line" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:schema>

////////////////////////////////////////////////////////////

[6122 byte] By [sun@2005a] at [2007-11-27 8:00:20]
# 1
This is exactly the same problem (requiring the same solution) as the one you mentioned in your previous topic: http://forum.java.sun.com/thread.jspa?threadID=5185498&tstart=0
prgguya at 2007-7-12 19:42:16 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
You are saying correct, i did fact.setNamespaceAware(true);but after doing this, still it is not working.
sun@2005a at 2007-7-12 19:42:16 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...