服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

jdom与xml的案例

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 (instanceof element) {
                demo((elemento);
            }
            else if (instanceof comment)
        docomment((commento);
            else if (instanceof processinginstruction
        dopi((processinginstructiono);
        }
    }     

    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 (instanceof element) {
                demo((elemento);
            }
            else if (instanceof comment
        docomment((comment)o);
            else if (instanceof processinginstruction
        dopi((processinginstruction)o);
            else if (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>


*/

扫描关注微信公众号