• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How read with JDOM

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code I have to read a XML file:
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.io.IOException;
import java.util.*;

public class FibRead {
public static void main(String[] args) {

if (args.length == 0) {
System.out.println("Usage: java FibRead URL");
return;
}

SAXBuilder builder = new SAXBuilder();

try {
Document doc = builder.build(args[0]);
listNodes(doc, 0);
}
// indicates a well-formedness error
catch (JDOMException e) {
System.out.println(args[0] + " is not well-formed.");
System.out.println(e.getMessage());
}
//catch (IOException e) {
// System.out.println(e);
//}

}


public static void listNodes(Object o, int depth) {

printSpaces(depth);

if (o instanceof Element) {
Element element = (Element) o;
System.out.println("Element: " + element.getName());
List children = element.getContent();
Iterator iterator = children.iterator();
while (iterator.hasNext()) {
Object child = iterator.next();
listNodes(child, depth+1);
}
}
else if (o instanceof Document) {
System.out.println("Document");
Document doc = (Document) o;
List children = doc.getContent();
Iterator iterator = children.iterator();
while (iterator.hasNext()) {
Object child = iterator.next();
listNodes(child, depth+1);
}
}
else if (o instanceof Comment) {
System.out.println("Comment");
}
else if (o instanceof CDATA) {
System.out.println("CDATA section");
// CDATA is a subclass of Text so this test must come
// before the test for Text.
}
else if (o instanceof Text) {
System.out.println("Text");
}
else if (o instanceof EntityRef) {
System.out.println("Entity reference");
}
else if (o instanceof ProcessingInstruction) {
System.out.println("Processing Instruction");
}
else { // This really shouldn't happen
System.out.println("Unexpected type: " + o.getClass());
}

}

private static void printSpaces(int n) {

for (int i = 0; i < n; i++) {
System.out.print(' ');
}

}
}
It works well except in the listnodes function, I would like it to print out the values of the objects, Text, Comment, CData. Currently it only print the elements
 
Dean Reedy
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EVeryone,
I have answered my own question:
else if (o instanceof CDATA) {
CDATA temp = (CDATA) o;
System.out.println("CDATA section:" + temp.getText());
// CDATA is a subclass of Text so this test must come
// before the test for Text.
}
else if (o instanceof Text) {
Text temp = (Text) o;
System.out.println("Text" + temp.getText());
Thanks anyway.
Dean
 
There is no "i" in denial. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic