jdom読み書きxmlファイルの例

8301 ワード

事前にここからダウンロードする必要があります。http://www.jdom.org/dist/binary/ jdom.jar
JdomRWXML.javaソース:
import java.io.FileInputStream;   
import java.io.FileOutputStream;   
import java.io.IOException;   
import java.util.List;   
  
import org.jdom.Document;   
import org.jdom.Element;   
import org.jdom.JDOMException;   
import org.jdom.input.SAXBuilder;   
import org.jdom.output.XMLOutputter;   
  
public class JdomRWXML {   
    public void BuildXMLDoc() throws IOException, JDOMException {   
        //       list;   
        Element root = new Element("list");   
        //           ;   
        Document Doc = new Document(root);   
        //    for                     ;   
        for (int i = 0; i < 5; i++) {   
            //       company;   
            Element elements = new Element("company");   
            //   company        id;   
            elements.setAttribute("id", "" + i);   
            //   company              
            elements   
                    .addContent(new Element("company_name").setText("name" + i));   
            elements.addContent(new Element("company_email").setText("name" + i   
                    + "@163.com"));   
            //     list  company   ;   
            root.addContent(elements);   
        }   
        XMLOutputter XMLOut = new XMLOutputter();   
        //   company_list.xml  ;   
        XMLOut.output(Doc, new FileOutputStream("company_list.xml"));   
    }   
  
    public void ReadXMLDoc() throws IOException, JDOMException {   
        SAXBuilder sb = new SAXBuilder();//         
        Document doc = sb.build(new FileInputStream("company_list.xml"));//          
        Element root = doc.getRootElement();//         
        List<?> list = root.getChildren();//              List    
        for (int i = 0; i < list.size(); i++) {   
            System.out.println("---------------------------");   
            Element item = (Element) list.get(i);//          
            String id = item.getAttribute("id").getValue();//         
            System.out.println("id-->" + id);   
  
            Element sub1 = item.getChild("company_name");//              
            String company_name = sub1.getText();//            
            System.out.println("company_name-->" + company_name);   
  
            Element sub2 = item.getChild("company_email");   
            String company_email = sub2.getText();   
            System.out.println("company_email-->" + company_email);   
        }   
  
    }   
  
    /**   
     * @param args   
     */  
    public static void main(String[] args) {   
        // TODO Auto-generated method stub   
        try {   
            JdomRWXML jdomRWXML = new JdomRWXML();   
  
            System.out.println("--------BuildXMLDoc----------");   
            jdomRWXML.BuildXMLDoc();   
  
            System.out.println("--------ReadXMLDoc----------");   
            jdomRWXML.ReadXMLDoc();   
  
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   
  
} 
jdomを使ってxml文書を読み、書きます。
import java.io.FileInputStream;   
import java.io.FileOutputStream;   
import java.util.List;   
  
import org.jdom.Document;   
import org.jdom.Element;   
import org.jdom.input.SAXBuilder;   
import org.jdom.output.XMLOutputter;   
  
public class XMLFile   
{   
    String id = "";   
    String indent = ""; //        
    SAXBuilder sb = new SAXBuilder();//        
    boolean newLines = true; //          
    XMLOutputter outp = new XMLOutputter ();    
    public static void main(String[] args)   
    {   
  
    }   
  
    public void removeContent(String fileName,int pos) //               0     
    {   
        try  
        {   
            Document doc = sb.build(new FileInputStream(fileName));//         
            Element MyElement = doc.getRootElement(); //        
            List<?> listTwo = MyElement.getChildren();//  list,              
            listTwo.remove(pos); //     
            outp.output(doc,new FileOutputStream(fileName)); //    2.XML      
        }   
        catch(Exception e)   
        {   
            e.printStackTrace();   
        }   
    }   
    public void modifyContent(String fileName,int pos,String name,String content)//   XML     
    {   
        try  
        {   
            Document doc = sb.build(new FileInputStream(fileName));//         
            Element root = doc.getRootElement();//        
            List<?> list = root.getChildren();//             List    
            Element item = (Element)list.get(pos); //                    
            item.getChild(name).setText(content);   
            outp.output(doc,new FileOutputStream(fileName)); //    2.XML      
        }   
        catch(Exception e)   
        {   
            e.printStackTrace();   
        }   
           
    }   
    public void listContent(String fileName) //  ,   XML     
    {   
        try  
        {   
            Document doc=sb.build(new FileInputStream(fileName));//         
            Element root=doc.getRootElement();//        
            List<?> list=root.getChildren();//             List    
            for(int i=0;i<list.size();i++)   
            {               
                Element item = (Element)list.get(i);//         
                String name = item.getAttribute("id").getValue();//        
                System.out.println("id: "+name);   
                Element sub = item.getChild("title");//             
                String text = sub.getText();//           
                System.out.println("Title: "+text);   
                Element sub2 = item.getChild("content");   
                String text2 = sub2.getText();   
                System.out.println("Content: "+text2);   
                Element sub3 = item.getChild("time");   
                String text3 = sub3.getText();   
                System.out.println("Time: "+text3);   
                id = name;   
            }   
        }   
        catch(Exception e)   
        {   
            e.printStackTrace();   
        }   
    }   
    public void addContent(String fileName,String title,String content,String time) // XML      .   
    {   
        try  
        {   
            Document doc=sb.build(new FileInputStream(fileName));//         
            Element root=doc.getRootElement();//        
            List<?> list=root.getChildren();//             List    
            Element article = new Element("message");   
            article.setAttribute("id", (list.size()+2)+"");   
            article.addContent(new Element("title").setText(title));   
            article.addContent(new Element("content").setText(content));   
            article.addContent(new Element("time").setText(time));   
            doc.getRootElement().addContent(article);   
            outp.output(doc, new FileOutputStream(fileName)); //    2.XML      
        }   
        catch(Exception e)   
        {   
            e.printStackTrace();   
        }   
    }   
  
}