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.