關(guān)于jdom的解析

          Posted on 2006-10-06 00:09 開關(guān) 閱讀(416) 評論(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: 關(guān)于jdom的解析  回復(fù)  更多評論   

          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>

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 开封县| 兰西县| 宁远县| 郎溪县| 凤山市| 博野县| 西华县| 阿拉善左旗| 太仓市| 青海省| 兖州市| 苗栗市| 婺源县| 兴宁市| 阳高县| 江阴市| 郯城县| 海盐县| 鹰潭市| 江西省| 阿城市| 扎赉特旗| 辉南县| 武邑县| 常山县| 曲沃县| 财经| 渭南市| 桃江县| 镶黄旗| 尼勒克县| 奎屯市| 邳州市| 元谋县| 太和县| 元氏县| 贵溪市| 筠连县| 聂拉木县| 棋牌| 周口市|