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 James Ostheimer <jo...@alumni.virginia.edu> on 2006/11/27 17:27:20 UTC
Problem with Xerces DOM (newbie)
Hi-
I have been using java for quite a while, but this is my first experience
with the DOM parser and Xerces. My issue is that I can't seem to get the
parser to parse a very simple XML file. Here is the XML (pulled directly
from the debugger):
<?xml version="1.0" encoding="UTF-8"?>
<LoginResults>
<Accepted>
true
</Accepted>
</LoginResults>
Here is the code I am trying to execute:
protected Document parseXmlFile(WebResponse response) throws
ParserConfigurationException, IOException, SAXException{
//get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//dbf.setValidating(true);
// dbf.setIgnoringElementContentWhitespace(true);
//dbf.setNamespaceAware(true);
//Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();
String text = response.getText().trim();
ByteArrayInputStream bais = new ByteArrayInputStream(
text.getBytes());
//parse using builder to get DOM representation of the XML file
InputSource source = new InputSource(bais);
source.setEncoding("UTF-8");
Document dom = db.parse(bais);
Element docEle = dom.getDocumentElement();
//get a nodelist of elements
NodeList nl = docEle.getElementsByTagName("Accepted");
return dom;
//return db.parse(source);
}
The NodeList nl, is empty (verified by eclipse debugger). This I just do
not uderstand, the XML is well-formed and very simple and I should be able
to get something by tag name even without a DTD. I do have whitespace
issues of course, and I understand that. Also note that this XML string
works fine on the client side javascript (XML is produced by a servlet).
Does anyone have any idea what I am doing wrong? I worked on this for 8
hours yesterday and just cannot get the DOM parser to work for this very
simple XML. I am using the latest version of Xerces 2.9.0.
James
Re: Problem with Xerces DOM (newbie)
Posted by James Ostheimer <jo...@alumni.virginia.edu>.
Got that one already :)
James
On 11/27/06, Wax, Ed <Ed...@csgsystems.com> wrote:
>
> James,
> Adding trim() to the getNodeValue() method will resolve your white space
> issues as well, as opposed to any parser configuration setting.
>
>
> if (node.getNodeValue().*trim().*equalsIgnoreCase("true"))
>
> Ed
>
> -----Original Message-----
> *From:* james.ostheimer@gmail.com [mailto:james.ostheimer@gmail.com] *On
> Behalf Of *James Ostheimer
> *Sent:* Monday, November 27, 2006 10:58 AM
> *To:* j-users@xerces.apache.org
> *Subject:* Re: Problem with Xerces DOM (newbie)
>
> Thanks to Peter and Ed for the reply they helped me figure out that I
> shouldn't have trusted the debugger in this case!
>
> I got it working now!
>
> James
>
> On 11/27/06, Peter McCracken <pe...@ca.ibm.com> wrote:
> >
> >
> > Hi James,
> >
> > james.ostheimer@gmail.com wrote on 27/11/2006 11:27:20 AM:
> > > I have been using java for quite a while, but this is my first
> > > experience with the DOM parser and Xerces. My issue is that I can't
> > > seem to get the parser to parse a very simple XML file.
> >
> > I tried out the code sample you provided (with the exception that I hard
> > coded the String variable instead of using a WebResponse object) and it
> > worked fine for me, using Xerces 2.9.0. I assume you've verified that
> > the "text" variable contains the XML that you expect it to contain? Have
> > you tried executing the piece of code that you sent as a standalone
> > application to verify that it's really that section that's causing the
> > problem?
> >
> > > The NodeList nl, is empty (verified by eclipse debugger). This I
> > > just do not uderstand, the XML is well-formed and very simple and I
> > > should be able to get something by tag name even without a DTD.
> >
> > One caveat is that the NodeList object will look like it is empty in a
> > debugger when you first get it. For performance reasons, Xerces defers
> > filling the NodeList until you actually ask for nodes from the list. If you
> > call getLength() on the NodeList, it will walk the entire DOM tree and fully
> > populate the list. If you call item(N) without calling getLength(), Xerces
> > will find only the first N items without parsing the entire XML tree. This
> > is useful in cases where you're processing a large DOM tree but only want
> > the first occurrence of a particular tag.
> >
> > Cheers,
> > --
> > Peter McCracken
> > XML Parser Development
> > IBM Toronto Lab
> > Email: peterjm@ca.ibm.com
>
>
>
RE: Problem with Xerces DOM (newbie)
Posted by "Wax, Ed" <Ed...@csgsystems.com>.
James,
Adding trim() to the getNodeValue() method will resolve your white space
issues as well, as opposed to any parser configuration setting.
if (node.getNodeValue().trim().equalsIgnoreCase("true"))
Ed
-----Original Message-----
From: james.ostheimer@gmail.com
[mailto:james.ostheimer@gmail.com] On Behalf Of James Ostheimer
Sent: Monday, November 27, 2006 10:58 AM
To: j-users@xerces.apache.org
Subject: Re: Problem with Xerces DOM (newbie)
Thanks to Peter and Ed for the reply they helped me figure out
that I shouldn't have trusted the debugger in this case!
I got it working now!
James
On 11/27/06, Peter McCracken <pe...@ca.ibm.com> wrote:
Hi James,
james.ostheimer@gmail.com wrote on 27/11/2006 11:27:20
AM:
> I have been using java for quite a while, but this is
my first
> experience with the DOM parser and Xerces. My issue
is that I can't
> seem to get the parser to parse a very simple XML
file.
I tried out the code sample you provided (with the
exception that I hard coded the String variable instead of using a
WebResponse object) and it worked fine for me, using Xerces 2.9.0. I
assume you've verified that the "text" variable contains the XML that
you expect it to contain? Have you tried executing the piece of code
that you sent as a standalone application to verify that it's really
that section that's causing the problem?
> The NodeList nl, is empty (verified by eclipse
debugger). This I
> just do not uderstand, the XML is well-formed and very
simple and I
> should be able to get something by tag name even
without a DTD.
One caveat is that the NodeList object will look like it
is empty in a debugger when you first get it. For performance reasons,
Xerces defers filling the NodeList until you actually ask for nodes from
the list. If you call getLength() on the NodeList, it will walk the
entire DOM tree and fully populate the list. If you call item(N)
without calling getLength(), Xerces will find only the first N items
without parsing the entire XML tree. This is useful in cases where
you're processing a large DOM tree but only want the first occurrence of
a particular tag.
Cheers,
--
Peter McCracken
XML Parser Development
IBM Toronto Lab
Email: peterjm@ca.ibm.com
Re: Problem with Xerces DOM (newbie)
Posted by James Ostheimer <jo...@alumni.virginia.edu>.
Thanks to Peter and Ed for the reply they helped me figure out that I
shouldn't have trusted the debugger in this case!
I got it working now!
James
On 11/27/06, Peter McCracken <pe...@ca.ibm.com> wrote:
>
>
> Hi James,
>
> james.ostheimer@gmail.com wrote on 27/11/2006 11:27:20 AM:
> > I have been using java for quite a while, but this is my first
> > experience with the DOM parser and Xerces. My issue is that I can't
> > seem to get the parser to parse a very simple XML file.
>
> I tried out the code sample you provided (with the exception that I hard
> coded the String variable instead of using a WebResponse object) and it
> worked fine for me, using Xerces 2.9.0. I assume you've verified that the
> "text" variable contains the XML that you expect it to contain? Have you
> tried executing the piece of code that you sent as a standalone application
> to verify that it's really that section that's causing the problem?
>
> > The NodeList nl, is empty (verified by eclipse debugger). This I
> > just do not uderstand, the XML is well-formed and very simple and I
> > should be able to get something by tag name even without a DTD.
>
> One caveat is that the NodeList object will look like it is empty in a
> debugger when you first get it. For performance reasons, Xerces defers
> filling the NodeList until you actually ask for nodes from the list. If you
> call getLength() on the NodeList, it will walk the entire DOM tree and fully
> populate the list. If you call item(N) without calling getLength(), Xerces
> will find only the first N items without parsing the entire XML tree. This
> is useful in cases where you're processing a large DOM tree but only want
> the first occurrence of a particular tag.
>
> Cheers,
> --
> Peter McCracken
> XML Parser Development
> IBM Toronto Lab
> Email: peterjm@ca.ibm.com
RE: Problem with Xerces DOM (newbie)
Posted by "Bullotta, Rick" <ri...@sap.com>.
Try it with a StringReader instead of a byte array input stream, and let
us know the result (perhaps an encoding issue?)
- Rick
________________________________
From: james.ostheimer@gmail.com [mailto:james.ostheimer@gmail.com] On
Behalf Of James Ostheimer
Sent: Monday, November 27, 2006 11:27 AM
To: j-users@xerces.apache.org
Subject: Problem with Xerces DOM (newbie)
Hi-
I have been using java for quite a while, but this is my first
experience with the DOM parser and Xerces. My issue is that I can't
seem to get the parser to parse a very simple XML file. Here is the XML
(pulled directly from the debugger):
<?xml version="1.0" encoding="UTF-8"?>
<LoginResults>
<Accepted>
true
</Accepted>
</LoginResults>
Here is the code I am trying to execute:
protected Document parseXmlFile(WebResponse response) throws
ParserConfigurationException, IOException, SAXException{
//get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance
();
//dbf.setValidating(true);
// dbf.setIgnoringElementContentWhitespace(true);
//dbf.setNamespaceAware(true);
//Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();
String text = response.getText().trim();
ByteArrayInputStream bais = new
ByteArrayInputStream(text.getBytes());
//parse using builder to get DOM representation of the XML
file
InputSource source = new InputSource(bais);
source.setEncoding("UTF-8");
Document dom = db.parse(bais);
Element docEle = dom.getDocumentElement();
//get a nodelist of elements
NodeList nl = docEle.getElementsByTagName("Accepted");
return dom;
//return db.parse(source);
}
The NodeList nl, is empty (verified by eclipse debugger). This I just
do not uderstand, the XML is well-formed and very simple and I should be
able to get something by tag name even without a DTD. I do have
whitespace issues of course, and I understand that. Also note that this
XML string works fine on the client side javascript (XML is produced by
a servlet).
Does anyone have any idea what I am doing wrong? I worked on this for 8
hours yesterday and just cannot get the DOM parser to work for this very
simple XML. I am using the latest version of Xerces 2.9.0.
James
Re: Problem with Xerces DOM (newbie)
Posted by Peter McCracken <pe...@ca.ibm.com>.
Hi James,
james.ostheimer@gmail.com wrote on 27/11/2006 11:27:20 AM:
> I have been using java for quite a while, but this is my first
> experience with the DOM parser and Xerces. My issue is that I can't
> seem to get the parser to parse a very simple XML file.
I tried out the code sample you provided (with the exception that I hard
coded the String variable instead of using a WebResponse object) and it
worked fine for me, using Xerces 2.9.0. I assume you've verified that the
"text" variable contains the XML that you expect it to contain? Have you
tried executing the piece of code that you sent as a standalone
application to verify that it's really that section that's causing the
problem?
> The NodeList nl, is empty (verified by eclipse debugger). This I
> just do not uderstand, the XML is well-formed and very simple and I
> should be able to get something by tag name even without a DTD.
One caveat is that the NodeList object will look like it is empty in a
debugger when you first get it. For performance reasons, Xerces defers
filling the NodeList until you actually ask for nodes from the list. If
you call getLength() on the NodeList, it will walk the entire DOM tree and
fully populate the list. If you call item(N) without calling getLength(),
Xerces will find only the first N items without parsing the entire XML
tree. This is useful in cases where you're processing a large DOM tree
but only want the first occurrence of a particular tag.
Cheers,
--
Peter McCracken
XML Parser Development
IBM Toronto Lab
Email: peterjm@ca.ibm.com