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

Discussion in 'Java' started by BeGreen, Aug 13, 2006.

  1. BeGreen

    BeGreen Guest

    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!

    see below a sample of my xml file:



    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <root>
    <Record>
    <PartNo>01</PartNo>
    <weight/>
    <Record>
    <Record>
    <PartNo>02</PartNo>


    <weight>A</weight>
    </Record>
    </root>
     
    BeGreen, Aug 13, 2006
    #1
    1. Advertising

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

    BeGreen wrote:
    > 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!
    >
    > see below a sample of my xml file:
    >
    >
    >
    > <?xml version="1.0" encoding="ISO-8859-1" ?>
    > <root>
    > <Record>
    > <PartNo>01</PartNo>
    > <weight/>
    > <Record>
    > <Record>
    > <PartNo>02</PartNo>
    >
    >
    > <weight>A</weight>
    > </Record>
    > </root>


    Java parses that XML just fine.

    Assuming you change the bad <Record> to </Record> so
    the XML becomes wellformed !

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Aug 13, 2006
    #2
    1. Advertising

  3. BeGreen

    BeGreen Guest

    Arne, sorry for that. Actually I forgot the the forward slash on the
    record, it's:

    <Record>
    <PartNo>01</PartNo>
    <weight/>
    </Record>



    My only problem is to parse an empty tag with this format <weight/>




    <Record> t</Record>
    Arne Vajhøj wrote:
    > BeGreen wrote:
    > > 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!
    > >
    > > see below a sample of my xml file:
    > >
    > >
    > >
    > > <?xml version="1.0" encoding="ISO-8859-1" ?>
    > > <root>
    > > <Record>
    > > <PartNo>01</PartNo>
    > > <weight/>
    > > <Record>
    > > <Record>
    > > <PartNo>02</PartNo>
    > >
    > >
    > > <weight>A</weight>
    > > </Record>
    > > </root>

    >
    > Java parses that XML just fine.
    >
    > Assuming you change the bad <Record> to </Record> so
    > the XML becomes wellformed !
    >
    > Arne
     
    BeGreen, Aug 13, 2006
    #3
  4. BeGreen

    BeGreen Guest

    Arne, sorry for that. Actually I forgot the forward slash on the
    record, it's:
    ...............................................................................
    <Record>
    <PartNo>01</PartNo>
    <weight/>
    </Record>
    ..............................................................................


    My only problem is to parse an empty tag with this format <weight/>



    > Arne Vajhøj wrote:
    > > BeGreen wrote:
    > > > 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!
    > > >
    > > > see below a sample of my xml file:
    > > >
    > > >
    > > >
    > > > <?xml version="1.0" encoding="ISO-8859-1" ?>
    > > > <root>
    > > > <Record>
    > > > <PartNo>01</PartNo>
    > > > <weight/>
    > > > <Record>
    > > > <Record>
    > > > <PartNo>02</PartNo>
    > > >
    > > >
    > > > <weight>A</weight>
    > > > </Record>
    > > > </root>

    > >
    > > Java parses that XML just fine.
    > >
    > > Assuming you change the bad <Record> to </Record> so
    > > the XML becomes wellformed !
    > >
    > > Arne
     
    BeGreen, Aug 13, 2006
    #4
  5. Re: DocumentBuilder's parse method is not able to read a tag suchas 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
     
    Michael Rauscher, Aug 13, 2006
    #5
  6. Michael Rauscher wrote:
    > BeGreen schrieb:

    ....
    > > 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/>.

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

    .....
    > System.out.println("Done.");
    > }
    > }


    :) That was a lovely little ..example. ;)

    Andrew T.
     
    Andrew Thompson, Aug 13, 2006
    #6
  7. Re: DocumentBuilder's parse method is not able to read a tag suchas this one <weight/>

    BeGreen wrote:
    > My only problem is to parse an empty tag with this format <weight/>


    That is not a problem.

    Java XML parsers do not have a problem with that.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Aug 13, 2006
    #7
  8. Re: DocumentBuilder's parse method is not able to read a tag suchas this one <weight/>

    Andrew Thompson schrieb:
    >> import java.io.*;
    >> import javax.xml.parsers.*;

    > ....
    >> System.out.println("Done.");
    >> }
    >> }

    >
    > :) That was a lovely little ..example. ;)


    Please provide a sscce ;)

    Bye
    Michael
     
    Michael Rauscher, Aug 13, 2006
    #8
  9. BeGreen

    BeGreen Guest

    Hi Michael,

    Yes, I can be able to parse the xml data, but my program is failing
    when executing the code below, to retrieve the Record where there is
    this empty tag without a value, <weight/>

    <weight>something</weight> can be retrieved, but why not this one
    <weight/>

    As far as I am concerned <weight/> is a well-formed empty tag, and it
    should be retrieved successfully!

    There must an XML/Jave API to be able to retrieve it!


    ...............................................................................................

    if (node.getNodeName()=="weight") {
    String weightvalue = node.getFirstChild().getNodeValue();
    }

    -----------------------------------------------------------------------------------------------

    For your info, Node is the object of the class: org.w3c.dom.Node





    see below a sample of my xml file:


    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <root>
    <Record>
    <PartNo>01</PartNo>
    <weight/>
    </Record>
    <Record>
    <PartNo>02</PartNo>


    <weight>A</weight>
    </Record>
    </root>
     
    BeGreen, Aug 13, 2006
    #9
  10. BeGreen

    BeGreen Guest

    Arne,

    See below my respone to Michael.

    Yes, I can be able to parse the xml data, but to retrieve this kind of
    empty tag <weight/> is my problem.

    <weight> something </weight> can be retrieved though!

    Thanks!


    Arne Vajhøj wrote:
    > BeGreen wrote:
    > > My only problem is to parse an empty tag with this format <weight/>

    >
    > That is not a problem.
    >
    > Java XML parsers do not have a problem with that.
    >
    > Arne
     
    BeGreen, Aug 13, 2006
    #10
  11. BeGreen wrote:
    ....
    > Yes, I can be able to parse the xml data, but my program is failing


    With what exact exception or error message?
    Please copy/paste it (or at least, the first few lines).

    > if (node.getNodeName()=="weight") {
    > String weightvalue = node.getFirstChild().getNodeValue();
    > }

    ....
    > For your info, Node is the object of the class: org.w3c.dom.Node


    For my info., an SSCCE* serves better..

    But note that the API documentation mentions that
    the second method called might not return an Object,
    therefore the call of the third method on the null
    returned by the second method might iteslf
    result in a NullPointerException.

    ( BTW -Multiple statements on one line, is not
    the best strategy for debugging broken code. )

    There are a variety of other things that might be
    happening, best examined in that SSCCE.

    * <http://www.physci.org/codes/sscce>

    Andrew T.
     
    Andrew Thompson, Aug 13, 2006
    #11
  12. Re: DocumentBuilder's parse method is not able to read a tag suchas this one <weight/>

    BeGreen schrieb:
    > Hi Michael,
    >
    > Yes, I can be able to parse the xml data, but my program is failing
    > when executing the code below, to retrieve the Record where there is
    > this empty tag without a value, <weight/>


    > ..............................................................................................
    >
    > if (node.getNodeName()=="weight") {
    > String weightvalue = node.getFirstChild().getNodeValue();
    > }
    >
    > -----------------------------------------------------------------------------------------------
    >
    > For your info, Node is the object of the class: org.w3c.dom.Node


    Note, org.w3c.dom.Node is an interface.

    It's correct that you can't get the first child of an empty element: an
    empty element is empty.

    Change your code to something like:

    if ( node.getNodeName()=="weight" ) {
    String weightValue = (node.hasChildNodes()
    ? node.getFirstChild().getNodeValue()
    : "EMPTY");
    }

    Bye
    Michael
     
    Michael Rauscher, Aug 13, 2006
    #12
  13. Re: DocumentBuilder's parse method is not able to read a tag suchas this one <weight/>

    On 13-8-2006 19:53, BeGreen wrote:
    > Hi Michael,
    >
    > Yes, I can be able to parse the xml data, but my program is failing
    > when executing the code below, to retrieve the Record where there is
    > this empty tag without a value, <weight/>
    >
    > <weight>something</weight> can be retrieved, but why not this one
    > <weight/>
    >
    > As far as I am concerned <weight/> is a well-formed empty tag, and it
    > should be retrieved successfully!
    >
    > There must an XML/Jave API to be able to retrieve it!
    >
    >
    > ..............................................................................................
    >
    > if (node.getNodeName()=="weight") {
    > String weightvalue = node.getFirstChild().getNodeValue();
    > }
    >
    > -----------------------------------------------------------------------------------------------


    An empty tag has no child nodes. Find out how many children a node has
    by using node.getChildNodes().getLength();
    See Michael's example below in which I've extended to obtain the value
    of the <weight> tags.


    import java.io.*;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;

    public class XMLTest {
    public static 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();
    Document doc = builder.parse(is);
    is.close();
    inspect(doc);
    System.out.println("Done.");
    }

    private static void inspect(Node node) {
    if (node != null) {
    if ("weight".equals(node.getNodeName())) {
    String weightValue;
    NodeList children = node.getChildNodes();
    if (children.getLength() > 0) {
    weightValue = node.getFirstChild().getNodeValue();
    }
    else
    {
    weightValue = "#empty#";
    }
    System.out.println(node + " " + weightValue);
    }

    NodeList children = node.getChildNodes();
    int n = children.getLength();
    for (int i = 0; i < n; i++) {
    inspect(children.item(i));
    }
    }
    }
    }



    --
    Regards,

    Roland
     
    Roland de Ruiter, Aug 13, 2006
    #13
  14. Re: DocumentBuilder's parse method is not able to read a tag suchas this one <weight/>

    BeGreen wrote:
    > Yes, I can be able to parse the xml data, but to retrieve this kind of
    > empty tag <weight/> is my problem.


    >See below my respone to Michael.


    You can find the tag.

    You can not find the non existing value inside the tag.

    To me Java acts very logical.

    if (node.getNodeName()=="weight") {
    String weightvalue = node.getFirstChild().getNodeValue();
    }

    I can not see what node.getFirstChild() can return for
    <weight/> except null.

    And calling a method on null gives an exception.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Aug 13, 2006
    #14
  15. BeGreen

    BeGreen Guest

    Hi Guys,

    Thanks for your help. I finally fixed it, with your sample codes.

    Roland de Ruiter wrote:
    > On 13-8-2006 19:53, BeGreen wrote:
    > > Hi Michael,
    > >
    > > Yes, I can be able to parse the xml data, but my program is failing
    > > when executing the code below, to retrieve the Record where there is
    > > this empty tag without a value, <weight/>
    > >
    > > <weight>something</weight> can be retrieved, but why not this one
    > > <weight/>
    > >
    > > As far as I am concerned <weight/> is a well-formed empty tag, and it
    > > should be retrieved successfully!
    > >
    > > There must an XML/Jave API to be able to retrieve it!
    > >
    > >
    > > ..............................................................................................
    > >
    > > if (node.getNodeName()=="weight") {
    > > String weightvalue = node.getFirstChild().getNodeValue();
    > > }
    > >
    > > -----------------------------------------------------------------------------------------------

    >
    > An empty tag has no child nodes. Find out how many children a node has
    > by using node.getChildNodes().getLength();
    > See Michael's example below in which I've extended to obtain the value
    > of the <weight> tags.
    >
    >
    > import java.io.*;
    > import javax.xml.parsers.*;
    > import org.w3c.dom.*;
    >
    > public class XMLTest {
    > public static 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();
    > Document doc = builder.parse(is);
    > is.close();
    > inspect(doc);
    > System.out.println("Done.");
    > }
    >
    > private static void inspect(Node node) {
    > if (node != null) {
    > if ("weight".equals(node.getNodeName())) {
    > String weightValue;
    > NodeList children = node.getChildNodes();
    > if (children.getLength() > 0) {
    > weightValue = node.getFirstChild().getNodeValue();
    > }
    > else
    > {
    > weightValue = "#empty#";
    > }
    > System.out.println(node + " " + weightValue);
    > }
    >
    > NodeList children = node.getChildNodes();
    > int n = children.getLength();
    > for (int i = 0; i < n; i++) {
    > inspect(children.item(i));
    > }
    > }
    > }
    > }
    >
    >
    >
    > --
    > Regards,
    >
    > Roland
     
    BeGreen, Aug 14, 2006
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike
    Replies:
    0
    Views:
    8,202
  2. David
    Replies:
    0
    Views:
    600
    David
    May 27, 2004
  3. David
    Replies:
    1
    Views:
    2,181
    Filip Larsen
    Jun 6, 2004
  4. Ed
    Replies:
    6
    Views:
    1,327
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Aug 2, 2007
  5. John L.
    Replies:
    3
    Views:
    924
    Stanimir Stamenkov
    Jan 1, 2013
Loading...

Share This Page