You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by John Gouveia <jg...@camara.co.za> on 2001/05/17 10:52:56 UTC
Problems with parsing a String to a Document
Hi everyone
I am new to XML. I am parsing a String into a Document. The string is parsed into the document and the tags are found but the problem is the elements are not parsed. If I access the root node by document.getDocumentElement() it just returns null. But it will still recognise the root element by the tag name. I am using Xerces version 1.3.1.
Here is my code:
StringReader stringReader = new StringReader(xml);
InputSource inputSource = new InputSource(stringReader);
DOMParser parser = new DOMParser();
try {
parser.parse(inputSource);
} catch (SAXException se) {
System.out.println("Could not load document");
se.printStackTrace();
} catch (IOException ioe) {
System.out.println("Could not load document");
ioe.printStackTrace();
}
Document document = parser.getDocument();
System.out.println(document.getDocumentElement()); // outputs this -> [orderProviderList: null]
my xml data"
<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<orderProviderList><startDate>15-Apr-2001</startDate><endDate>25-Apr-2001</endDate><order><order_no>10002</order_no>
<buyer_name>Heymer</buyer_name><order_date>16/04/2001 08:05</order_date><order_total>R0.00</order_total>
</order><orderProviderList>
Could anyone help me with this
thanks
Re: Problems with parsing a String to a Document
Posted by Henry Kleynhans <he...@obsidianlabs.com>.
Hi John,
This works as expected. Your root element does not have any text data
in it, only other elements. If you want to get your data try doing
an xpath query or iterating throught the child elemtemts.
Best regards,
Henry
> John Gouveia wrote:
>
> Hi everyone
>
> I am new to XML. I am parsing a String into a Document. The string is parsed
> into the document and the tags are found but the problem is the elements are
> not parsed. If I access the root node by document.getDocumentElement() it just
> returns null. But it will still recognise the root element by the tag name. I
> am using Xerces version 1.3.1.
>
>
> Here is my code:
>
> StringReader stringReader = new StringReader(xml);
> InputSource inputSource = new InputSource(stringReader);
> DOMParser parser = new DOMParser();
>
> try {
> parser.parse(inputSource);
> } catch (SAXException se) {
> System.out.println("Could not load document");
> se.printStackTrace();
> } catch (IOException ioe) {
> System.out.println("Could not load document");
> ioe.printStackTrace();
> }
>
>
> Document document = parser.getDocument();
>
> System.out.println(document.getDocumentElement()); // outputs
> this -> [orderProviderList: null]
>
> my xml data"
>
> <?xml version=\"1.0\" encoding=\"UTF-8\" ?>
> <orderProviderList><startDate>15-Apr-2001</startDate><endDate>25-Apr-2001</endDate><order><order_no>10002</order_no>
> <buyer_name>Heymer</buyer_name><order_date>16/04/2001
> 08:05</order_date><order_total>R0.00</order_total>
> </order><orderProviderList>
>
> Could anyone help me with this
>
> thanks
--
Henry Kleynhans Obsidian Labs
Systems Engineer South Africa
henry@obsidianlabs.com Tel: +27 11 792 6500
http://www.obsidianlabs.com/ Fax: +27 11 792 6522
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org