关于jdom的解析

Posted on 2006-10-06 00:09 开关 阅读(402) 评论(1)  编辑  收藏 所属分类: XML

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class CreateJdomXml {
 String root="messages";
    String child="message";
 String[][] childitem={{"title","a"},{"content","aaa"},{"email","aaaaa"}};
 /**
  * @param args
  */
 public CreateJdomXml(){
  
 }
 public static void main(String[] args) {
  // TODO 自动生成方法存根
  CreateJdomXml cj=new CreateJdomXml();
  cj.deletexml();
  
 }
   
 public void createxml(){
  Element roote=new Element(root);
  Element childe=new Element(child);
  childe.addAttribute("id", "A");
  for(int i=0;i<childitem.length;i++){
   Element childchild=new Element(childitem[i][0]);
   childchild.setText(childitem[i][1]);
   childe.addContent(childchild);
  }
  roote.addContent(childe);
  Document doc=new Document(roote);
  String newfile="D:/NewCearteExample.xml";
  File file=new File(newfile);
  if(!file.exists()){
   try {
    file.createNewFile();
   } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
   }
  }
  OutputStream os=null;
  try {
   os=new FileOutputStream(file);
  } catch (FileNotFoundException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
  XMLOutputter xop=new XMLOutputter();
  xop.setEncoding("gb2312");
  xop.setIndentSize(3);
  xop.setNewlines(true);
  xop.setSuppressDeclaration(false);
  try {
   xop.output(doc, os);
   os.close();
     
  } catch (IOException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
  
  
 }
 
 public void updatexml(){
  InputStream is=null;
  OutputStream os=null;
 String path="D:/NewCearteExample.xml";
 File file=new File(path);
 try {
  is=new FileInputStream(file);
  SAXBuilder builder=new SAXBuilder();
  Document doc=builder.build(is);
  Element root=doc.getRootElement();
  List childrenlist=root.getChildren();
  Element child=(Element)childrenlist.get(0);
  Element childchild=new Element("desc");
  childchild.setText("person");
  child.addContent(childchild);
       
  os=new FileOutputStream(file);
  XMLOutputter xop=new XMLOutputter();
  xop.setNewlines(true);
  xop.setIndentSize(3);
  xop.setEncoding("gb2312"); 
  xop.output(doc,os);
  os.close();
  is.close();
  
  
 } catch (Exception e) {
  // TODO 自动生成 catch 块
  e.printStackTrace();
 }
  
 
 }

   public void deletexml(){
    InputStream is=null;
  OutputStream os=null;
 String path="D:/NewCearteExample.xml";
 File file=new File(path); 
 try{
 is=new FileInputStream(file);
 SAXBuilder builder=new SAXBuilder();
 Document doc=builder.build(is);
 Element root=doc.getRootElement();
 List childlist=root.getChildren();
 Element child=(Element)childlist.get(0);
 List childrenlist=child.getChildren();
 for(int i=0;i<childrenlist.size();i++){
  Element childchild=(Element)childrenlist.get(i);
  if(childchild.getName().equalsIgnoreCase("desc")){
   childrenlist.remove(i);
  }
 }
 os=new FileOutputStream(file);
 XMLOutputter xop=new XMLOutputter();
 xop.setNewlines(true);
 xop.setIndentSize(3);
 xop.setEncoding("gb2312"); 
 xop.output(doc,os);
 os.close();
 is.close();
 
 }catch(Exception e)
 {e.printStackTrace();
 }
   }
 
}

Feedback

# re: 关于jdom的解析  回复  更多评论   

2009-11-27 05:35 by anon
have you looked at vtd-xml, which is the latest and more advanced/powerful XML Processing API

<a href="http://vtd-xml.sf.net">vtd-xml</a>

只有注册用户登录后才能发表评论。


网站导航: