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 / First Aid / November 2006

Tip: Looking for answers? Try searching our database.

Help Reading XML file

Thread view: 
petedawn@gmail.com - 01 Nov 2006 23:56 GMT
guys,

i have a xml file with this format,
<a>
    <b name="test0" value="0">
             <c name="test1"/>
             <c name="test2"/>
             <d>
                     <e name="test3" value="1"/>
                     <f name="test4" value="2"/>
             </d>
    </b>
</a>

basically i have a file which is multiple levels deep and has
corresponding element attributes. can somebody please help me out to
read the individual attributes from each line. i am new to XML and am
having a tough time with it. thanks.
petedawn@gmail.com - 02 Nov 2006 00:01 GMT
i have got this so far but it only reads the first level and doesnt go
any deeper.

DocumentBuilderFactory docBuilderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("configuration.xml"));

doc.getDocumentElement ().normalize ();

Node n;
NodeList nodes = doc.getDocumentElement().getChildNodes();

for( int i=0 ; i<nodes.getLength(); i++ )
     {
              n = nodes.item( i );

              if( n.getNodeType() == Node.ELEMENT_NODE )
              {
                  if( n.getNodeName().equals( "b" ) )
                  {
                  NamedNodeMap attrs = n.getAttributes();
                     String s = attrs.getNamedItem("name").getNodeValue();
                     String s2 =
attrs.getNamedItem("value").getNodeValue();
                        }
                   }
      }

i am able to read the first bit, which is <b name="test0" value="0">
but how do i get to the next level .thanks.
Kevin Mess - 02 Nov 2006 15:20 GMT
> i have got this so far but it only reads the first level and doesnt go
> any deeper.
[quoted text clipped - 27 lines]
> i am able to read the first bit, which is <b name="test0" value="0">
> but how do i get to the next level .thanks.

Hi Pete.  I see you're trying to build a DOM tree in memory.  I -may-
be able to assist, but I should self-disclos that I have more practical
experience using JDOM (which is not to say I'm an expert by any means!).

Can you explain what you're trying to do?  Is a DOM tree better for you
than sequentially parsing the file using SAX?  How large is the XML
file?  Do you need to bind data structures the information contained
within the XML?

Kevin
petedawn@gmail.com - 03 Nov 2006 00:09 GMT
hi kevin,

thanks for your reply. i am happy to using DOM or just SAX parsing. all
i want to do is be able to read the individual element attributes
multiple levels deep. if you think SAX is better then its fine, but i
dont want to complicate things early on. i want to learn something
simple first and then move on to more complex things.

so either solution is fine, as long as it works. thanks.
petedawn@gmail.com - 03 Nov 2006 00:14 GMT
my XML file is fairly small. just a small ascii file say 500 lines long
max.
Kevin Mess - 03 Nov 2006 15:55 GMT
> i have got this so far but it only reads the first level and doesnt go
> any deeper.
[quoted text clipped - 27 lines]
> i am able to read the first bit, which is <b name="test0" value="0">
> but how do i get to the next level .thanks.

Since this is a tree structure, the easiest way to traverse it would be
with recursion:

   public static void main(String[] args)
           throws ParserConfigurationException, IOException, SAXException {
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       Document doc = factory.newDocumentBuilder().parse(new
File("/Users/kevin/Desktop/configuration.xml"));
       traverse(doc, 0);
   }

    // note recursion
   public static void traverse(Node node, int level) {
       // Process node here, then move on to the next child

       // If there are any children, visit each one
       NodeList list = node.getChildNodes();
       for (int i = 0; i < list.getLength(); i++) {
           // Get child node
           Node childNode = list.item(i);

           // Visit child node
           traverse(childNode, level + 1);
       }
   }

There is NO error checking for null pointers in this code; you'll need
to adapt this.

Kevin
petedawn@gmail.com - 06 Nov 2006 01:19 GMT
Cheers mate. Now how do i get each individual element and its
corresponding attribute value. i just need to break up the information
into individual parts. i guess i just need to convert something to a
string and print it out. any help.
petedawn@gmail.com - 06 Nov 2006 02:55 GMT
mate, this code is picking up everything i need, so thats good. but its
not organising it into sections.

i mean that its picking up all attributes which are called name. i need
to be able to differentiate between b's attributes and d's attributes.
i hope i am able to explain myself, all i want to do is be able to
relate individual element attributes with their parent.

thanks.
Kevin Mess - 06 Nov 2006 22:07 GMT
> mate, this code is picking up everything i need, so thats good. but its
> not organising it into sections.
[quoted text clipped - 5 lines]
>
> thanks.

Note this snippet of code:

 public static void traverse(Node node, int level) {

     // Process node here, then move on to the next child
     // for example -

     if (node.getParentNode().getNodeName().equals("b")) {    <<<<<
         // or whatever,  blah blah blah                        <<<<<
     }

     // If there are any children, visit each one
     NodeList list = node.getChildNodes();
     for (int i = 0; i < list.getLength(); i++) {
         // Get child node
         Node childNode = list.item(i);

         // Visit child node
         traverse(childNode, level + 1);
     }
 }

Where you see the comment // Process node here ... is where you would
put code to handle your data.

I still seriously suggest that you investigate JDOM - it was designed
specifically for _Java_ handling of XML documents (unlike DOM or SAX),
and so its methods are far easier to work with.

Some (quick and hasty) code I wrote using JDOM might show where this is easier:

  public QuakeData(String rssURL, Point p)
        throws IOException, JDOMException, ParseException {

        // retrieve xml and build document in memory
       SAXBuilder builder = new SAXBuilder();
       Document doc = builder.build(rssURL);
        // that's it - the entire xml file is now in memory.

        // this xml has three namespaces in all, so set up the extra two
       Namespace geo =
               Namespace.getNamespace("geo",
"http://www.w3.org/2003/01/geo/wgs84_pos#");
       Namespace dc =
               Namespace.getNamespace("dc",
"http://purl.org/dc/elements/1.1/");

        // some working variables
       List subjects;
       Element eventElement, subjectElement;

        // find the root
       Element root = doc.getRootElement();

        // we're only interested in the the branch of the tree with "item"
        // notice we can specify by _name_ here
       List events = root.getChild("channel").getChildren("item");

        // now that we have only the branch of the tree with "item", iterate through
        // and work the data the way we want. In this case, I'm creating an
new object
        // of class Event, and then setting its attributes based on the xml
data. Notice
        // again I just use the name specified in the xml document - very simple:
       for (Object event : events) {
           QuakeEvent quakeEvent = new QuakeEvent();
           eventElement = (Element) event;
           quakeEvent.setPubDate(eventElement.getChildText("pubDate"));
           quakeEvent.setTitle(eventElement.getChildText("title"));
           
quakeEvent.setDescription(eventElement.getChildText("description"));
           quakeEvent.setLink(eventElement.getChildText("link"));
           quakeEvent.setLocation(eventElement.getChildText("lat", geo),
                   eventElement.getChildText("long", geo));
           subjects = eventElement.getChildren("subject", dc);

        // subject as multiple occurrences, all named the same thing. I need to
        // isolate them differently then previously.  Depending upon their
array index,
        // I set the class attribute accordingly.
           for (int i = 0; i < subjects.size(); i++) {
               subjectElement = (Element) subjects.get(i);
               switch (i) {
                   case 0: {
                       quakeEvent.setMagnitude(subjectElement.getText());
                       break;
                   }
                   case 1: {
                       quakeEvent.setPeriod(subjectElement.getText());
                       break;
                   }
                   case 2: {
                       quakeEvent.setDepth(subjectElement.getText());
                       break;
                   }
                   default:
                       break;
               }
           }

        // all the class attributes have been set, so now I store this object in my
        // ArrayList.
           quakeList.add(quakeEvent);
       }

All the best to you if you keep using DOM - I gave up on it in favor of JDOM :)

Kevin


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.