Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Using xerces to parse a string of xml does not seem to work

Reply
Thread Tools

Using xerces to parse a string of xml does not seem to work

 
 
Watsh
Guest
Posts: n/a
 
      11-02-2004
Hi All,

I have been trying to parse an XML string using the StringReader and
InputSource interface but the document returned to me is always null.
Please find the code below which i have been experimenting with:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.InputSource;
import org.w3c.dom.Document;
import java.io.StringReader;

public class Test {

/** Creates a new instance of Test */
public Test()
{
}
public static void main(String[] args)
{
try {
String xml = "<a><b/></a>";

DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
// set namespace and validating options on factory, if
necessary
factory.setValidating(false);
factory.setIgnoringComments(true);
DocumentBuilder builder = factory.newDocumentBuilder();
StringReader reader = new StringReader(xml);
InputSource source = new InputSource(reader);
Document document = builder.parse(source);
System.out.println(document.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}

Any pointers on how to resolve this issue will be of great help also
any tip for an alternative way is just as well fine for me.

Warm Regards,
Watsh
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      11-02-2004


Watsh wrote:

> I have been trying to parse an XML string using the StringReader and
> InputSource interface but the document returned to me is always null.
> Please find the code below which i have been experimenting with:
>
> import javax.xml.parsers.DocumentBuilderFactory;
> import javax.xml.parsers.DocumentBuilder;
> import org.xml.sax.InputSource;
> import org.w3c.dom.Document;
> import java.io.StringReader;
>
> public class Test {
>
> /** Creates a new instance of Test */
> public Test()
> {
> }
> public static void main(String[] args)
> {
> try {
> String xml = "<a><b/></a>";
>
> DocumentBuilderFactory factory =
> DocumentBuilderFactory.newInstance();
> // set namespace and validating options on factory, if
> necessary
> factory.setValidating(false);
> factory.setIgnoringComments(true);
> DocumentBuilder builder = factory.newDocumentBuilder();
> StringReader reader = new StringReader(xml);
> InputSource source = new InputSource(reader);
> Document document = builder.parse(source);
> System.out.println(document.toString());


toString is not reliable to find out whether the document has been
parsed as I think the result of toString depends on the
implementation/parser used, it can return anything the parser
implementor wants it to return.
Check
System.out.println(document.getElementsByTagName(" *"))
and I am sure it shows that there are elements in the document.

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Keith M. Corbett
Guest
Posts: n/a
 
      11-02-2004
"Watsh" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> String xml = "<a><b/></a>";


What happens if you prepend an XML declaration?

/kmc


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to parse an xml message in std::string (C++) using xerces sax2 muralibala68@gmail.com XML 2 09-06-2007 02:32 PM
<location> element does not seem to work - Does anybody know how to solve this. alexvodovoz@yahoo.com ASP .Net 1 05-25-2007 01:12 AM
XML HTTP GET does not seem to work in IE6 when used with setTimeout Robert S Javascript 2 07-23-2006 12:32 PM
Webservice works once and then DOES NOT seem to work even though program does not crash Phi! ASP .Net Web Services 1 04-23-2004 08:42 AM
(very new to XML) Relative SystemLiteral does not seem to work Kiwi XML 2 09-18-2003 02:29 PM



Advertisments