Problems parsing XML

Robert Stober stober at iag.net
Tue Apr 14 17:40:18 BST 1998


Hi all,

I've been working on a java xml browser application. I got the base code
from the book "XML Complete" by Steven Holzner. Some of the code shown
below comes directly from that book, then I show my modifications.

I am using the msxml parser version 1.8. The book I referred to to was
using an earlier version and the packages have changed. In fact Java
itself may have changed. This code is from the aforementioned book page
231 and is supposed to get the document root, then it's children, then
it should enumerate over those children.

  void showRecord(int recordNumber)
    {
        Element root = d.getRoot();
        Element elem = null, elem2 = null;
        Enumeration enum = root.getChildren();

        for(int index = 0; index <= recordNumber; index++){
            elem = (Element)enum.nextElement();
        }

        Enumeration enum2 = elem.getChildren();
        for(int index = 0; index < 2; index++){

            elem2 = (Element)enum2.nextElement();

            if (elem2.getTagName().equals("FIRSTNAME")) {
                text1.setText(elem2.getText());
            }

            if (elem2.getTagName().equals("LASTNAME")) {
                text2.setText(elem2.getText());
            }
        }
    }

Using Sun's JDK 1.1.5 this code gives errors: explicit cast need to
convert class ElementCollection to java.util.Enumeration. This is
because the line:

       Enumeration enum = root.getChildren(); // getChildren return
ElementCollection

getChildren() returns an object of type ElementCollection not
Enumeration. So I made some changes to the code as shown below:

void showRecord(int recordNumber)
    {
        Element root = d.getRoot();
        Element elem = null, elem2 = null;
 ElementEnumeration enum = new ElementEnumeration(root);

        for(int index = 0; index <= recordNumber; index++) {
            elem = (Element)enum.nextElement();
 }

 ElementEnumeration enum2 = new ElementEnumeration(elem);
        for(int index = 0; index < 2; index++) {

         elem2 = (Element)enum2.nextElement();

             if (elem2.getTagName().equals("FIRSTNAME")) {
                 text1.setText(elem2.getText());
             }

             if (elem2.getTagName().equals("LASTNAME")) {
                 text2.setText(elem2.getText());
             }
 }

So now I'm using the msxml ElementEnumeration class which seems like it
should work. And it compiles just fine. But when I run it something goes
wrong...

Document root is: com.ms.xml.om.ElementImpl[tag=DOCUMENT, type=0,
text=null]
elem is: com.ms.xml.om.ElementImpl[tag=null,type=12, text=
]
java.lang.NullPointerException:
    at employees.showRecord(employees.java:111)
    at employees.init(employees.java:70)

Here's the xml document I'm trying to parse. You'll see that the next
tag is NAME not null!

<?xml version = "1.0" ?>
<!DOCTYPE DOCUMENT [
<!ELEMENT DOCUMENT (NAME)*>
<!ELEMENT NAME (LASTNAME, FIRSTNAME)>
<!ELEMENT LASTNAME (#PCDATA)>
<!ELEMENT FIRSTNAME (#PCDATA)>
]>
<DOCUMENT>
<NAME>
    <LASTNAME>Franklin</LASTNAME>
    <FIRSTNAME>Tom</FIRSTNAME>
</NAME>
<NAME>
    <LASTNAME>Guertin</LASTNAME>
    <FIRSTNAME>Phoebe</FIRSTNAME>
</NAME>
<NAME>
    <LASTNAME>Johnson</LASTNAME>
    <FIRSTNAME>Frank</FIRSTNAME>
</NAME>
<NAME>
    <LASTNAME>Tomlin</LASTNAME>
    <FIRSTNAME>Brenda</FIRSTNAME>
</NAME>
<NAME>
    <LASTNAME>Edwards</LASTNAME>
    <FIRSTNAME>Tina</FIRSTNAME>
</NAME>
</DOCUMENT>

Okay, so what am I doing wrong? Has anybody else run into this?

Any help any of you could provide will be greatly appreciated!

Robert Stober
stober at iag.net
stoberrm at orl.wec.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: vcard.vcf
Type: text/x-vcard
Size: 376 bytes
Desc: Card for Robert Stober
Url : http://mailman.ic.ac.uk/pipermail/xml-dev/attachments/19980414/28f15aff/vcard.vcf


More information about the Xml-dev mailing list