關于jdom的解析

          Posted on 2006-10-06 00:09 開關 閱讀(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: 關于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>

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


          網站導航:
           
          主站蜘蛛池模板: 武宁县| 莆田市| 和静县| 拜城县| 宁津县| 晋江市| 泉州市| 山阴县| 镇康县| 黄浦区| 达孜县| 阿鲁科尔沁旗| 白河县| 洛阳市| 明光市| 桐柏县| 青龙| 台南市| 东乌珠穆沁旗| 濮阳县| 麻江县| 耒阳市| 巴东县| 富阳市| 河津市| 定襄县| 普兰县| 永州市| 将乐县| 台南市| 巫溪县| 右玉县| 普兰县| 塔河县| 武安市| 来凤县| 三穗县| 伊宁市| 永安市| 乐亭县| 钟山县|