资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

JDom如何新建和读取xml-创新互联

这篇文章主要介绍“JDom如何新建和读取xml”,在日常操作中,相信很多人在JDom如何新建和读取xml问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”JDom如何新建和读取xml”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

成都创新互联-专业网站定制、快速模板网站建设、高性价比济阳网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式济阳网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖济阳地区。费用合理售后完善,10年实体公司更值得信赖。

//package xml;
import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;

//import org.jdom.*;
import org.jdom.Attribute;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class test {

private static String titlename;

private String chapter;

private String topic;

public static void main(String [] msg){
   wriXml();
   // try{
    readXml();
 //    }catch (Exception e){e.printStackTrace();}
}
public static void readXml() {  
   SAXBuilder builder=new SAXBuilder();
  try{
   Document document=builder.build(new File("contents.xml"));
   Element root=document.getRootElement();
   Element title=root.getChild("title");
   System.out.println(title.getText());  
 
   Element contents=root.getChild("contents");
   List lis=contents.getChildren("chapter");
   Iterator it=lis.iterator();
   while(it.hasNext()){
      Element chapter=(Element) it.next();
      System.out.println(chapter.getAttribute("title"));
    }
   }catch(Exception e){e.printStackTrace();}
   
   
}

public static void wriXml(){

Element ele=new Element("boot");
   Document doc=new Document(ele);
   
   ele.addAttribute(new Attribute("vin","123456789"));
   ele.addContent(new Element("first").addContent("guoxb"));
   ele.addContent(new Element("second").addContent("30"));
   ele.addContent(new Element("third").addContent("univercity"));
   ele.addContent(new Element("first").addContent("girlfri").addAttribute("name","22"));      
   ele.addContent(new Element("right"));
   ele.addContent(new Comment("这是一条主释"));
   Element gg=ele.getChild("right");
       gg.addContent(new Element("pkg").addContent("thhhhhhhh"));
   
   //访问子元素
   Element fir=ele.getChild("first");
   //去处子元素
   boolean remo=ele.removeChild("first");
   
   //打印输出xml
   try{
     XMLOutputter out=new XMLOutputter(" ",true);
       out.setEncoding("gb2312");
       out.output(doc,System.out);

         
   //保存到xml文档中
   FileWriter fi=new FileWriter("./gxb.xml");
   out.output(doc,fi);
   fi.close();         }catch (Exception e){e.printStackTrace();}
 }
}

[@more@]

到此,关于“JDom如何新建和读取xml”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联-成都网站建设公司网站,小编会继续努力为大家带来更多实用的文章!


网页标题:JDom如何新建和读取xml-创新互联
文章位置:http://cdkjz.cn/article/pecpj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220