Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
BeGreen - 13 Aug 2006 03:21 GMT
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>
Arne Vajhøj - 13 Aug 2006 03:36 GMT
> javax.xml.parsers.DocumentBuilder's parse method is not able to parse
> the xml file, when
[quoted text clipped - 18 lines]
>   </Record>
> </root>

Java parses that XML just fine.

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

Arne
BeGreen - 13 Aug 2006 03:42 GMT
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>
> > javax.xml.parsers.DocumentBuilder's parse method is not able to parse
> > the xml file, when
[quoted text clipped - 25 lines]
>
> Arne
BeGreen - 13 Aug 2006 03:46 GMT
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/>

> > > javax.xml.parsers.DocumentBuilder's parse method is not able to parse
> > > the xml file, when
[quoted text clipped - 25 lines]
> >
> > Arne
Arne Vajhøj - 13 Aug 2006 15:43 GMT
> 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 - 13 Aug 2006 18:59 GMT
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!

> > My only problem is to parse an empty tag with this format <weight/>
>
[quoted text clipped - 3 lines]
>
> Arne
Arne Vajhøj - 13 Aug 2006 20:46 GMT
> 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
Michael Rauscher - 13 Aug 2006 04:48 GMT
BeGreen schrieb:
> Hi All,
>
[quoted text clipped - 5 lines]
> 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
Andrew Thompson - 13 Aug 2006 07:14 GMT
> 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.
Michael Rauscher - 13 Aug 2006 16:45 GMT
Andrew Thompson schrieb:
>> import java.io.*;
>> import javax.xml.parsers.*;
[quoted text clipped - 4 lines]
>
> :-)  That was a lovely little ..example.    ;)

Please provide a sscce ;)

Bye
Michael
BeGreen - 13 Aug 2006 18:53 GMT
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>
Andrew Thompson - 13 Aug 2006 19:33 GMT
...
> 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.
Michael Rauscher - 13 Aug 2006 19:48 GMT
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/>

> ..............................................................................................
>
[quoted text clipped - 5 lines]
>
> 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
Roland de Ruiter - 13 Aug 2006 19:52 GMT
> Hi Michael,
>
[quoted text clipped - 17 lines]
>
> -----------------------------------------------------------------------------------------------

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));
            }
        }
    }
}

Signature

Regards,

Roland

BeGreen - 14 Aug 2006 00:28 GMT
Hi Guys,

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

> > Hi Michael,
> >
[quoted text clipped - 75 lines]
>      }
> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.