import java.io.ioexception;
import java.util.iterator;
import java.util.list;
import org.jdom.comment;
import org.jdom.document;
import org.jdom.element;
import org.jdom.jdomexception;
import org.jdom.processinginstruction;
import org.jdom.input.dombuilder;
import org.jdom.input.saxbuilder;
import org.jdom.output.domoutputter;
/*
* simple demo of jdom
*/
public class jdomdemo {
public static void main(string[] args) {
// must be at least one file or url argument
if (args.length == 0) {
system.out.println("usage: java jdomdemo url [...]");
}
saxbuilder saxbuilder = new saxbuilder();
dombuilder dombuilder = new dombuilder();
for (int i = 0; i < args.length; i++) {
try {
document jdomdocument = saxbuilder.build(args[i]);
domoutputter domoutputter = new domoutputter();
/*
* test getting dom document from jdom document
org.w3c.dom.document domdocument = domoutputter.output(doc);
*/
/*
* test getting dom element from jdom element
*/
org.w3c.dom.element domelement =
domoutputter.output(jdomdocument.getrootelement());
/*
* test getting jdom element from dom element
*/
org.jdom.element jdomelement = dombuilder.build(domelement);
demo(jdomelement);
} catch (jdomexception e) { // indicates a well-formedness or other error
system.out.println(args[i] + " is not a well formed xml document.");
system.out.println(e.getmessage());
} catch (ioexception ex) {
system.out.println("input or output error:" +
args[i] + ": " + ex);
}
}
}
public static void demo(document doc) {
list children = doc.getcontent();
iterator iterator = children.iterator();
while (iterator.hasnext()) {
object o = iterator.next();
if (o instanceof element) {
demo((element) o);
}
else if (o instanceof comment)
docomment((comment) o);
else if (o instanceof processinginstruction)
dopi((processinginstruction) o);
}
}
public static void demo(element element) {
system.out.println("element " + element);
list attributes = element.getattributes();
list children = element.getcontent();
iterator iterator = children.iterator();
while (iterator.hasnext()) {
object o = iterator.next();
if (o instanceof element) {
demo((element) o);
}
else if (o instanceof comment)
docomment((comment)o);
else if (o instanceof processinginstruction)
dopi((processinginstruction)o);
else if (o instanceof string) {
system.out.println("string: " + o);
}
}
}
public static void docomment(comment c) {
system.out.println("comment: " + c);
}
public static void dopi(processinginstruction pi) {
system.out.println("pi: " + pi);
}
}
// demo xml file
/*
<?xml version="1.0"?>
<people>
<person>
<name>ian darwin</name>
<email>http://www.darwinsys.com/</email>
<country>canada</country>
</person>
<person>
<name>another darwin</name>
<email type="intranet">afd@node1</email>
<country>canada</country>
</person>
</people>
*/
import java.util.iterator;
import java.util.list;
import org.jdom.comment;
import org.jdom.document;
import org.jdom.element;
import org.jdom.jdomexception;
import org.jdom.processinginstruction;
import org.jdom.input.dombuilder;
import org.jdom.input.saxbuilder;
import org.jdom.output.domoutputter;
/*
* simple demo of jdom
*/
public class jdomdemo {
public static void main(string[] args) {
// must be at least one file or url argument
if (args.length == 0) {
system.out.println("usage: java jdomdemo url [...]");
}
saxbuilder saxbuilder = new saxbuilder();
dombuilder dombuilder = new dombuilder();
for (int i = 0; i < args.length; i++) {
try {
document jdomdocument = saxbuilder.build(args[i]);
domoutputter domoutputter = new domoutputter();
/*
* test getting dom document from jdom document
org.w3c.dom.document domdocument = domoutputter.output(doc);
*/
/*
* test getting dom element from jdom element
*/
org.w3c.dom.element domelement =
domoutputter.output(jdomdocument.getrootelement());
/*
* test getting jdom element from dom element
*/
org.jdom.element jdomelement = dombuilder.build(domelement);
demo(jdomelement);
} catch (jdomexception e) { // indicates a well-formedness or other error
system.out.println(args[i] + " is not a well formed xml document.");
system.out.println(e.getmessage());
} catch (ioexception ex) {
system.out.println("input or output error:" +
args[i] + ": " + ex);
}
}
}
public static void demo(document doc) {
list children = doc.getcontent();
iterator iterator = children.iterator();
while (iterator.hasnext()) {
object o = iterator.next();
if (o instanceof element) {
demo((element) o);
}
else if (o instanceof comment)
docomment((comment) o);
else if (o instanceof processinginstruction)
dopi((processinginstruction) o);
}
}
public static void demo(element element) {
system.out.println("element " + element);
list attributes = element.getattributes();
list children = element.getcontent();
iterator iterator = children.iterator();
while (iterator.hasnext()) {
object o = iterator.next();
if (o instanceof element) {
demo((element) o);
}
else if (o instanceof comment)
docomment((comment)o);
else if (o instanceof processinginstruction)
dopi((processinginstruction)o);
else if (o instanceof string) {
system.out.println("string: " + o);
}
}
}
public static void docomment(comment c) {
system.out.println("comment: " + c);
}
public static void dopi(processinginstruction pi) {
system.out.println("pi: " + pi);
}
}
// demo xml file
/*
<?xml version="1.0"?>
<people>
<person>
<name>ian darwin</name>
<email>http://www.darwinsys.com/</email>
<country>canada</country>
</person>
<person>
<name>another darwin</name>
<email type="intranet">afd@node1</email>
<country>canada</country>
</person>
</people>
*/
闽公网安备 35060202000074号