For Programmers: Free Programming Magazines  


Home > Archive > Java Help > October 2004 > org.xml.sax.SAXParseException: Document root element is missing.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author org.xml.sax.SAXParseException: Document root element is missing.
Kunal

2004-10-24, 3:56 am

Hi,
I get this error when i try to parse my XML file. The code is pasted
below and the XML file too is pasted. I don't understand why I am
getting this error even when the xml input seems correct.

Stack Trace :
org.xml.sax.SAXParseException: Document root element is missing.
at org.apache.crimson.parser.Parser2.fatal(Unknown Source)
at org.apache.crimson.parser.Parser2.fatal(Unknown Source)
at org.apache.crimson.parser.Parser2.parseInternal(Unknown Source)
at org.apache.crimson.parser.Parser2.parse(Unknown Source)
at org.apache.crimson.parser.XMLReaderImpl.parse(Unknown Source)
at org.apache.crimson.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at tsafe.test.SimpleTest.getExpectetValue(SimpleTest.java:139)
at tsafe.test.SimpleTest.blunderTest(SimpleTest.java:89)
at tsafe.engine.TsafeEngine.start(TsafeEngine.java:158)
at tsafe.client.TsafeClient.actionPerformed(TsafeClient.java:197)
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$DoPostEvent.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at tsafe.main.gui.utils.WaitCursorEventQueue.dispatchEvent(WaitCursorEventQueue.java:61)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown
Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown
Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Code :

private static String getExpectetValue(
String planeIdentifier,
String testCase)
throws IOException {
if (!new File(inputFile + ".xml").exists()) {
throw new IOException("Testinput file does not exist");
}

DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;

try {
//load input data
builder = fact.newDocumentBuilder();
Document doc = builder.parse(new File(inputFile + ".xml"));

//prerequisite is that the identifier in the file is unique

//Get all planes
NodeList elementListPlanes = doc.getElementsByTagName("plane");

//find the searched one
Element elementPlane =
getPlaneFromList(planeIdentifier, elementListPlanes);

//searched plane doesn't exist
if (elementPlane == null)
return null;

//get the test expected value
Element elementBlunder =
(Element) elementPlane.getElementsByTagName(testCase).item(0);

//if no testcase is in the input file
if (elementBlunder == null)
return null;

String s =
elementBlunder
.getElementsByTagName("expected")
.item(0)
.getChildNodes()
.item(0)
.getNodeValue();

return s;

} catch (ParserConfigurationException e) {
e.printStackTrace();
return null;
} catch (SAXException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}


Input file :
<?xml version="1.0" encoding="UTF-8" ?>
<planes>
<plane id="QW">
<TST001>
<expected>YES</expected>
</TST001>
</plane>
</planes>

Please suggest.

Thanks in advance.

Regards,
Kunal
Alex Kizub

2004-10-24, 3:56 am

I'm sure error is not in this code.
Probably it's in method
getPlaneFromList(planeIdentifier, elementListPlanes);
which you didn't post here.

What you was pasted runs fine.

Alex Kizub.

Kunal wrote:

> Hi,
> I get this error when i try to parse my XML file. The code is pasted
> below and the XML file too is pasted. I don't understand why I am
> getting this error even when the xml input seems correct.
>
> Stack Trace :
> org.xml.sax.SAXParseException: Document root element is missing.
> at org.apache.crimson.parser.Parser2.fatal(Unknown Source)
> at org.apache.crimson.parser.Parser2.fatal(Unknown Source)
> at org.apache.crimson.parser.Parser2.parseInternal(Unknown Source)
> at org.apache.crimson.parser.Parser2.parse(Unknown Source)
> at org.apache.crimson.parser.XMLReaderImpl.parse(Unknown Source)
> at org.apache.crimson.jaxp.DocumentBuilderImpl.parse(Unknown Source)
> at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
> at tsafe.test.SimpleTest.getExpectetValue(SimpleTest.java:139)
> at tsafe.test.SimpleTest.blunderTest(SimpleTest.java:89)
> at tsafe.engine.TsafeEngine.start(TsafeEngine.java:158)
> at tsafe.client.TsafeClient.actionPerformed(TsafeClient.java:197)
> at javax.swing.Timer.fireActionPerformed(Unknown Source)
> at javax.swing.Timer$DoPostEvent.run(Unknown Source)
> at java.awt.event.InvocationEvent.dispatch(Unknown Source)
> at java.awt.EventQueue.dispatchEvent(Unknown Source)
> at tsafe.main.gui.utils.WaitCursorEventQueue.dispatchEvent(WaitCursorEventQueue.java:61)
> at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown
> Source)
> at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown
> Source)
> at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
> at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
> at java.awt.EventDispatchThread.run(Unknown Source)
>
> Code :
>
> private static String getExpectetValue(
> String planeIdentifier,
> String testCase)
> throws IOException {
> if (!new File(inputFile + ".xml").exists()) {
> throw new IOException("Testinput file does not exist");
> }
>
> DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
> DocumentBuilder builder = null;
>
> try {
> //load input data
> builder = fact.newDocumentBuilder();
> Document doc = builder.parse(new File(inputFile + ".xml"));
>
> //prerequisite is that the identifier in the file is unique
>
> //Get all planes
> NodeList elementListPlanes = doc.getElementsByTagName("plane");
>
> //find the searched one
> Element elementPlane =
> getPlaneFromList(planeIdentifier, elementListPlanes);
>
> //searched plane doesn't exist
> if (elementPlane == null)
> return null;
>
> //get the test expected value
> Element elementBlunder =
> (Element) elementPlane.getElementsByTagName(testCase).item(0);
>
> //if no testcase is in the input file
> if (elementBlunder == null)
> return null;
>
> String s =
> elementBlunder
> .getElementsByTagName("expected")
> .item(0)
> .getChildNodes()
> .item(0)
> .getNodeValue();
>
> return s;
>
> } catch (ParserConfigurationException e) {
> e.printStackTrace();
> return null;
> } catch (SAXException e) {
> e.printStackTrace();
> return null;
> } catch (IOException e) {
> e.printStackTrace();
> return null;
> }
> }
>
> Input file :
> <?xml version="1.0" encoding="UTF-8" ?>
> <planes>
> <plane id="QW">
> <TST001>
> <expected>YES</expected>
> </TST001>
> </plane>
> </planes>
>
> Please suggest.
>
> Thanks in advance.
>
> Regards,
> Kunal


Kunal

2004-10-24, 3:57 pm

Hi Alex,
The error was due to the wrongpath from this file was getting read.
The problem i mentioned does not occur now, but you are right it is in
the method, which I think I can overcome.
Thanks,

kunal
Alex Kizub <akizub@yahoo.com> wrote in message news:<417B2B98.443FBF2@yahoo.com>...[color=darkred]
> I'm sure error is not in this code.
> Probably it's in method
> getPlaneFromList(planeIdentifier, elementListPlanes);
> which you didn't post here.
>
> What you was pasted runs fine.
>
> Alex Kizub.
>
> Kunal wrote:
>
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com