Code Comments
Programming Forum and web based access to our favorite programming groups.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(WaitCursorEventQu
eue.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
Post Follow-up to this messageI'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 Sourc
e)
> 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(WaitCur
sorEventQueue.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 exi
st");
> }
>
> DocumentBuilderFactory fact = DocumentBuilderFactory.newIn
stance();
> 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.getElementsByTagN
ame("plane");
>
> //find the searched one
> Element elementPlane =
> getPlaneFromList(planeIdentifier, elementL
istPlanes);
>
> //searched plane doesn't exist
> if (elementPlane == null)
> return null;
>
> //get the test expected value
> Element elementBlunder =
> (Element) elementPlane.getElementsByTagNam
e(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
Post Follow-up to this messageHi 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=d arkred] > 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: >
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.