![]() |
![]() | ![]() | ![]() | Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Using xerces to parse a string of xml does not seem to work [Thread Next] Re: Using xerces to parse a string of xml does not seem to workTo: NULL Date: 11/2/2004 1:14:00 PM
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/
| ![]() | ![]() | ![]() |
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | |||||
|
