⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdomtest.java

📁 java2 primer plus一书源程序
💻 JAVA
字号:
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;

import java.util.*;
import java.io.*;

public class JDOMTest {

  public static void showBooks( Element root ) {
       List books = root.getChildren( "book" );
       for( Iterator i=books.iterator(); i.hasNext(); ) {
         Element book = ( Element )i.next();
         System.out.println( "Book: " + book.getAttributeValue( "category" ) + ", " +
                             book.getChildTextTrim( "title" ) + ", " +
                             book.getChildTextTrim( "author" ) + ", " + 
                             book.getChildTextTrim( "price" ) );
       }
  }


  public static void main( String[] args ) {
    try {
       SAXBuilder builder = new SAXBuilder();
       Document doc = builder.build( "book.xml" );
       Element root = doc.getRootElement();
       System.out.println( "Book List Before: " );
       showBooks( root );

       // Add a new book
       Element newBook = new Element( "book" );
       newBook.setAttribute( "category", "fiction" );
       Element newTitle = new Element( "title" );
       newTitle.addContent( "Desecration" );
       Element newAuthor = new Element( "author" );
       newAuthor.addContent( "Tim LaHaye" );
       Element newPrice = new Element( "price" );
       newPrice.addContent( "19.95" );
       newBook.addContent( newTitle );
       newBook.addContent( newAuthor );
       newBook.addContent( newPrice );
       root.addContent( newBook );

       System.out.println( "Book List After: " );
       showBooks( root );

       XMLOutputter out = new XMLOutputter( "  ", true );
       out.output( root, System.out );

    }
    catch( Exception e ) {
       e.printStackTrace();
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -