Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
XML Hell (Just give me the value!)

Posted by John_Moylan (jfp), 24 April 2003
I don't ask much out of life, but an easy way of getting a text value from Java would be one of them.
I know I'm new to this Java lark, but am I missing something really obvious?

I just want the text value returned from each child nodes of <Row>

XML file:
Code:
<Import>
       <Row>
               <FileName>/producttesting/reports/nov2000pt30t32/01frontpage.html</FileName>
               <ReportTitle>Batteries</ReportTitle>
               <IndexTitle>Batteries</IndexTitle>
               <Summary>Buying advice plus tests on disposable and rechargeable batteries</Summary>
               <MainCategory>Audio visual</MainCategory>
               <SubCategory>Batteries</SubCategory>
               <Source>WH</Source>
       </Row>
       <Row>
               <FileName>/producttesting/reports/nov2000pt20t24/01frontpage.html</FileName>
               <ReportTitle>Camcorders</ReportTitle>
               <IndexTitle>Camcorders</IndexTitle>
               <Summary>Buying advice plus tests on ten high-quality camcorders</Summary>
               <MainCategory>Audio visual</MainCategory>
               <SubCategory>Camcorders</SubCategory>
               <Source>WH</Source>
       </Row>
       <Row>
               <FileName>/producttesting/reports/nov2001pt38t41/01frontpage.html</FileName>
               <ReportTitle>Camcorders</ReportTitle>
               <IndexTitle>Camcorders</IndexTitle>
               <Summary>Buying advice plus tests on 14 budget models</Summary>
               <BestBuyUpdate>/whichextra/content/computers_phones_internet/bbupdate.pdf</BestBuyUpdate>
               <MainCategory>Audio visual</MainCategory>
               <SubCategory>Camcorders</SubCategory>
               <Source>WH</Source>
       </Row>
       <Row>
               <FileName>/producttesting/reports/dec2001pt44t46/01frontpage.html</FileName>
               <ReportTitle>Computer video editing</ReportTitle>
               <IndexTitle>Computer video editing</IndexTitle>
               <Summary>Guide to editing videos on your computer</Summary>
               <MainCategory>Audio visual</MainCategory>
               <SubCategory>Camcorders</SubCategory>
               <Source>WH</Source>
       </Row>
</Import>


And the Java code is....sorry if its long

Code:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.IOException;
import org.w3c.dom.*;
import org.w3c.dom.NodeList;
import org.xml.sax.*;

public class DomTree {

   public static void main(String[] args) {

       DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
       DocumentBuilder builder = null;
       try {
           builder = builderFactory.newDocumentBuilder();
       }
       catch(ParserConfigurationException e){
           e.printStackTrace();
           System.exit(1);
       }

       System.out.println("Builder Factory = " + builderFactory + "\nBuilder = " + builder);

       File xmlFile = new File("./contentlist.xml");
       Document xmlDoc = null;

       try {
           xmlDoc = builder.parse(xmlFile);
       }
       catch(SAXException e) {
           e.printStackTrace();
           System.exit(1);
       }
       catch(IOException e) {
           e.printStackTrace();
           System.exit(1);
       }

       System.out.println("\nDocument body contents are:");
       listNodes(xmlDoc.getDocumentElement(), "");
   }

   static void listNodes(Node node, String indent) {

       String nodeName = node.getNodeName();
       NodeList list   = node.getChildNodes();

       if(list.getLength() > 0) {

           for(int i = 0; i < list.getLength(); i++) {

               listNodes(list.item(i), indent + " ");
               Node n = list.item(i);

               System.out.println(i + " : " + nodeName + " : Type is : " + n.getNodeName());

               if(n.getNodeName().equals("#text")) {

                   try {
                       Node kid = n.getFirstChild();
                       
                       // I THOUGHT THIS LINE BELOW WOULD DO THE JOB...
                       // BUT IT CAUSES AN EXCEPTION SO ITS COMMENTED OUT
                       //String value = kid.getNodeValue();
                       System.out.println("WHOOPI DO0, at least we know its a text node");
                   }
                   catch(DOMException dome) {

                       System.out.println("Bad Khama ");
                   }
               }
           }
       }
   }
}


Any pointers anyone?

Confused, frustrated, sad and lonely of Tonbridge Wells  

Posted by admin (Graham Ellis), 24 April 2003
Not got my Java books / crib sheets here with me, jfp .... I'm away until tomorrow evening.    The idea ofd Java is that it's harder the first time you do something, but the code you write is re-usable and so you get all that time investment back with interest later on!



Posted by John_Moylan (jfp), 24 April 2003
Well after much grief (I've been back and forwards on this for a day and a half) I managed to get a working model.

Seems I had more of a problem of understanding the how the DomTree works rather than a Java problem.

Anyway, for the benefit of someone else who may end up with the same grief, here goes

Code:
   static void listNodes(Node node, String indent) {

       String nodeName = node.getNodeName();
       NodeList list   = node.getChildNodes();
       System.out.println(" : " + nodeName);

       if(list.getLength() > 0) {

           for(int i = 0; i < list.getLength(); i++) {
                                                   
               Node n = list.item(i);
               NodeList tmp = n.getChildNodes();
               System.out.println("Node Type: " + n.getNodeType() + "\nNode Name: " + n.getNodeName());

               for (int j = 0; j < tmp.getLength(); j++) {

                   Node a = tmp.item(j);

                   try {
                       System.out.println("Name: " + a.getNodeName());                                                
                       System.out.println("Value: " + a.getFirstChild().getNodeValue());                                                                            
                   }
                   catch(Exception e) {
                       System.out.println("Bad Khama ");
                   }
               }
           }
       }
   }


Main is as first post.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho