Try it with a StringReader instead of a byte array input stream, and let
us know the result (perhaps an encoding issue?)

 

- Rick

________________________________

From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 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

Reply via email to