Re: DocumentBuilder's parse method is not able to read a tag such as this one <weight/>



BeGreen schrieb:
Hi All,

javax.xml.parsers.DocumentBuilder's parse method is not able to parse
the xml file, when
the record has an empty tag without a value, such as <weight/>.

Any Java class, you may know which can parse that tag successfully or
even ignore it?
Thanks!

import java.io.*;
import javax.xml.parsers.*;

public class XMLTest {
public static final void main( String args[] ) throws Exception {
String xml =
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
"<root>\n" +
" <Record>\n" +
" <PartNo>01</PartNo>\n" +
" <weight/>\n" +
" </Record>\n" +
" <Record>\n" +
" <PartNo>2</PartNo>\n" +
" <weight>A</weight>\n" +
" </Record>\n" +
"</root>";

System.out.println( xml );
byte data[] = xml.getBytes("ISO-8859-1");
ByteArrayInputStream is = new ByteArrayInputStream( data );

DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
builder.parse(is);
is.close();
System.out.println("Done.");
}
}

Bye
Michael
.



Relevant Pages

  • Re: Need more info about problem resolving entity reference
    ... to parse the REST response? ... and, like a good little SGML-derived XML parser, tries to fetch the DTD ... Well, first, that's an HTML doctype. ... you really don't want to keep fetching the DTDs like that. ...
    (comp.lang.perl.misc)
  • Re: Sending existing XML document to a document-literal web servic
    ... So the WSDL defines a type ... I don't want to parse the existing document and load the productOrderType ... My understanding is that you can use XmlSerializer to read the XML document ...
    (microsoft.public.dotnet.framework.aspnet.webservices)
  • Re: Need more info about problem resolving entity reference
    ... to parse the REST response? ... and, like a good little SGML-derived XML parser, tries to fetch the DTD ... Well, first, that's an HTML doctype. ... the XML you're actually trying to parse use external DTDs? ...
    (comp.lang.perl.misc)
  • Re: can someone review my code
    ... A particular version of glade UI ... parsing some subset of XML, ... wrote were indeed a simplified parser for simplified XML, ... code is able to parse anything at all); you can't get it right by ...
    (comp.lang.lisp)
  • Re: XML editor using MFC
    ... add elements to the tree control. ... XML tree is represented by an HTREEITEM. ... except for the CDATA stuff which is a real pain to parse. ... Error recovery: simplest form, when you find an error, throw a CException-derived class, ...
    (microsoft.public.vc.mfc)