OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Problems parsing XML

[ Lists Home | Date Index | Thread Index ]
  • From: Robert Stober <stober@iag.net>
  • To: xml-dev@ic.ac.uk
  • Date: Tue, 14 Apr 1998 10:40:07 +0000

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@iag.net
stoberrm@orl.wec.com
begin:          vcard
fn:             Robert Stober
n:              Stober;Robert
org:            Landrush Interactive Marketing
adr:            The Quadrangle;;440 Alafaya Trail;Orlando;Florida;32826;usa
email;internet: stober@iag.net
title:          President
tel;work:       (407) 249-7377
x-mozilla-cpt:  ;0
x-mozilla-html: TRUE
version:        2.1
end:            vcard





 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS