my dog learned polymorphism*
The moose likes XML and Related Technologies and the fly likes i have written code for dom but output given from the exception caught in the code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "i have written code for dom but output given from the exception caught in the code" Watch "i have written code for dom but output given from the exception caught in the code" New topic
Author

i have written code for dom but output given from the exception caught in the code

Atul Pawar
Greenhorn

Joined: Jan 31, 2009
Posts: 21
this is my XML code
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE ORDERS SYSTEM "orders.dtd">


<orders>
<order>
<customerid limit= "1000">12341</customerid>
<status>pending</status>
<item instock = "Y" itemid = "SA15">
<name>Silver Show Saddle, 16inch</name>
<price>825.00</price>
<qty>1</qty>
</item>
<item instock = "N" itemid = "C49">
<name>Premium Cinch</name>
<price>49.00</price>
<qty>1</qty>
</item>
</order>
<order>
<customerid limit = "150">251222</customerid>
<status>pending</status>
<item instock = "Y" itemid = "WB78">
<name>Winter Blanket(78 inch)</name>
<price>20.00</price>
<qty>10</qty>
</item>
</order>
</orders>



Now this is my java code

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.xml.sax.*;
import java.io.File;
import org.w3c.dom.Document;


import org.w3c.dom.Element;


public class OrderProcessor
{
public static void main(String[] args)
{
File docFile = new File("D:/MyProjects/orders.xml");
Document doc = null ;
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//
dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(docFile);
}
catch(javax.xml.parsers.ParserConfigurationException pce)
{
System.out.println("Parser was not configure correctly");
System.exit(1);
}
catch(java.io.IOException ioe)
{
System.out.println("Cannot read input file");
System.exit(1);
}
catch(org.xml.sax.SAXException se)
{
System.out.println("Problem parsing the file");
System.exit(1);
}
catch(java.lang.IllegalArgumentException ae)
{

System.out.println("Please specify an XML source");
System.exit(1);
}
//catch(Exception e)
//{
// System.out.println("Problem parsing the file " + e.getMessage());
//
}


}




and output is Problem parsing the file

what shud i do


"Here I am, World, do your worst!"
"It may sound like you’re inviting bad luck and catastrophe, but what this really means is that you are welcoming life’s challenges with open arms and are willing to bravely take them on."-Atul Pawar.
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

The SAXException instance should have more information about what caused the error. Try:
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12757
    
    5
You really should first catch SAXParseException.

Check the JavaDocs for SAXParseException - it can show you the line and column in the XML which caused the exception.


Bill
Atul Pawar
Greenhorn

Joined: Jan 31, 2009
Posts: 21
Carey Evans wrote:The SAXException instance should have more information about what caused the error. Try:



i have already used that exception in my catch loop and same exception is giving my declared output.
Problem parsing file
can any one give me exact solution.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41046
    
  43
Atul Pawar wrote: i have already used that exception in my catch loop and same exception is giving my declared output.

But you're not using the information that the exception can provide. If you did what William suggested (which was to read the javadocs of SAXParseException), you'd learn how to get more information about the problem.


Ping & DNS - my free Android networking tools app
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12757
    
    5
i have already used that exception in my catch loop


If I am going to go to the trouble to make a helpful suggestion, the least you can do is read the suggestion carefully. Jumping to conclusions is one of the leading causes of programmer stumbles.

Bill
Atul Pawar
Greenhorn

Joined: Jan 31, 2009
Posts: 21
William Brogden wrote:
i have already used that exception in my catch loop


If I am going to go to the trouble to make a helpful suggestion, the least you can do is read the suggestion carefully. Jumping to conclusions is one of the leading causes of programmer stumbles.

Bill


Yes GreenHorns are meant to scratch their heads...
Thanks for that........


D:\MyProjects>java OrderProcessor
org.xml.sax.SAXParseException: The markup declarations contained or pointed to by the document type declaration must be well
-formed.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
at org.apache.xerces.impl.XMLDTDScannerImpl.scanDecls(Unknown Source)
at org.apache.xerces.impl.XMLDTDScannerImpl.scanDTDExternalSubset(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at OrderProcessor.main(OrderProcessor.java:21)
Problem parsing the file



this was the output.

I also downloaded xerces.jar after this output but
and place it in jre/lib/ext also jdom and dom1.6.jar in same directory is it right to do so......

starting with dom learning is getting more lengthy.....

Thanks,

Regards,
Atul Pawar.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12757
    
    5
Now that you are catching SAXParseException you can find out exactly which part of the document is the problem.

Reading the JavaDocs we find the methods getLineNumber and getColumnNumber

catch( SAXParseException e ){
System.out.println("Parse error line: " + e.getLineNumber() + " column: " + e.getColumnNumber() );
e.printStackTrace();
System.exit(1);
}

However, you already have a hint as to the problem being in your DTD:
"The markup declarations contained or pointed to by the document type declaration must be well -formed."
You should probably look at that DTD first.

You should also spend some time browsing the JavaDocs.

Bill
Atul Pawar
Greenhorn

Joined: Jan 31, 2009
Posts: 21
William Brogden wrote:Now that you are catching SAXParseException you can find out exactly which part of the document is the problem.

Reading the JavaDocs we find the methods getLineNumber and getColumnNumber

catch( SAXParseException e ){
System.out.println("Parse error line: " + e.getLineNumber() + " column: " + e.getColumnNumber() );
e.printStackTrace();
System.exit(1);
}

However, you already have a hint as to the problem being in your DTD:
"The markup declarations contained or pointed to by the document type declaration must be well -formed."
You should probably look at that DTD first.

You should also spend some time browsing the JavaDocs.

Bill





Any body there
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12757
    
    5
My crystal ball is in the shop for the 10,000 vision checkup so I am unable to see the DTD document referred to in:

"The markup declarations contained or pointed to by the document type declaration must be well -formed."

and


I did get a vision of you checking the line and column information in the SAXParseException and finding that it also points to the DOCTYPE line as the cause of your problem.

Read this wikipedia entry for more about DTD formatting.

Bill
PS. The free trial version of XML SPY (download here) should be able to locate the problem in your DTD / XML. I bought a commercial copy years ago and never regretted it.
Atul Pawar
Greenhorn

Joined: Jan 31, 2009
Posts: 21
HI, Anybody got my issue.


William Brogden wrote:My crystal ball is in the shop for the 10,000 vision checkup so I am unable to see the DTD document referred to in:

"The markup declarations contained or pointed to by the document type declaration must be well -formed."

and


I did get a vision of you checking the line and column information in the SAXParseException and finding that it also points to the DOCTYPE line as the cause of your problem.

Read this wikipedia entry for more about DTD formatting.

Bill
PS. The free trial version of XML SPY (download here) should be able to locate the problem in your DTD / XML. I bought a commercial copy years ago and never regretted it.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41046
    
  43
Atul Pawar wrote:HI, Anybody got my issue.

Yes, we get it. But it seems that you're missing what William is trying to tell you: that the problem seems to be in the DTD, and since we don't have that, there's nothing we can do to help. We might be able to help if you posted the DTD. Of course, once you look at it, you may spot the problem yourself :-)
Atul Pawar
Greenhorn

Joined: Jan 31, 2009
Posts: 21
Ulf Dittmer wrote:
Atul Pawar wrote:HI, Anybody got my issue.

Yes, we get it. But it seems that you're missing what William is trying to tell you: that the problem seems to be in the DTD, and since we don't have that, there's nothing we can do to help. We might be able to help if you posted the DTD. Of course, once you look at it, you may spot the problem yourself :-)



yes thanks,
but actually i have only the xml file and java code but no dtd
i am really sorry but i dont have any knowledge on DOM or dtd. is it one more file how should i design it.

Sorry oncemore.

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12757
    
    5
Try just removing the DOCTYPE line from the XML file, it is not required.

Whoever authored the XML document had in mind that it would be used with a DTD - you should ask the author where the DTD is.

Bill
Atul Pawar
Greenhorn

Joined: Jan 31, 2009
Posts: 21
William Brogden wrote:Try just removing the DOCTYPE line from the XML file, it is not required.

Whoever authored the XML document had in mind that it would be used with a DTD - you should ask the author where the DTD is.

Bill


i STUDIED DTD AND MADE THIS DTD FOR MY orders.XML FILE NOW output after putting this directory orders.dtd

<!-- orders.dtd -->
<!ELEMENT orders(order)>
<!ELEMENT order(customerid,status,item)>
<!ELEMENT customerid>
<!ELEMENT status>
<!ELEMENT item (name,price,qty)>
<!ELEMENT price>
<!ELEMENT qty>


output is same

D:\MyProjects>java OrderProcessor orders.xml
Parse error line: 2 column: 3
org.xml.sax.SAXParseException: The markup declarations contained or pointed to by the document type declaration must be well
-formed.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
at org.apache.xerces.impl.XMLDTDScannerImpl.scanDecls(Unknown Source)
at org.apache.xerces.impl.XMLDTDScannerImpl.scanDTDExternalSubset(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at OrderProcessor.main(OrderProcessor.java:19)
Problem parsing the file





William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12757
    
    5
With the assistance of XML-SPY I located all the problems in your DTD.



Your formatting was not quite correct and you had left out the #PCDATA bits.

Bill
Atul Pawar
Greenhorn

Joined: Jan 31, 2009
Posts: 21
William Brogden wrote:With the assistance of XML-SPY I located all the problems in your DTD.



Your formatting was not quite correct and you had left out the #PCDATA bits.

Bill



Thanks a lot bill........
Atul Pawar
Greenhorn

Joined: Jan 31, 2009
Posts: 21
William Brogden wrote:With the assistance of XML-SPY I located all the problems in your DTD.



Your formatting was not quite correct and you had left out the #PCDATA bits.

Bill


At last the code compilled.
Iam really happy that i know what is DTD and how to test an xml through that.....
now help me understanding schemas.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: i have written code for dom but output given from the exception caught in the code
 
Similar Threads
How to get over DOM?
MySQL Transactions
Pretty printing XML
normalize( ) in DOM
Fresher ! need help