jdom学习(3)

Jdom使用详解及实例(3)
接上一节:

4.Attribute类:

xml 代码
  1. <table width="100%" border="0"> table>  

String width = table.getAttributeValue("width");//获得attribute

int border = table.getAttribute("width").getIntValue();

table.setAttribute("vspace", "0");//设置attribute

table.removeAttribute("vspace");// 删除一个或全部attribute

table.getAttributes().clear();

5.命名空间操作

<xhtml:html></xhtml:html>

 xmlns:xhtml="http://www.w3.org/1999/xhtml">

<xhtml:title>Home Page</xhtml:title>

Namespace xhtml = Namespace.getNamespace("xhtml", "http://www.w3.org/1999/xhtml");

List kids = html.getChildren("title", xhtml);

Element kid = html.getChild("title", xhtml);

kid.addContent(new Element("table", xhtml));

6.XSLT格式转换

使用以下函数可对XSLT转换

最后如果你需要使用w3cDocument则需要转换一下。

public static Document transform(String stylesheetDocument in)

                                        throws JDOMException {

     try {

       Transformer transformer = TransformerFactory.newInstance()

                             .newTransformer(new StreamSource(stylesheet));

       JDOMResult out = new JDOMResult();

       transformer.transform(new JDOMSource(in), out);

       return out.getDeocument();

     }

     catch (TransformerException e) {

       throw new JDOMException("XSLT Trandformation failed", e);

     }

   }


四.用例:

1、生成xml文档:

java 代码
  1. public class WriteXML{  
  2.   
  3.     public void BuildXML() throws Exception {  
  4.   
  5.         Element root,student,number,name,age;          
  6.   
  7.         root = new Element("student-info"); //生成根元素:student-info  
  8.   
  9.         student = new Element("student"); //生成元素:student(number,name,age)                              
  10.   
  11.         number = new Element("number");  
  12.   
  13.         name = new Element("name");  
  14.   
  15.         age = new Element("age");  
  16.   
  17.         Document doc = new Document(root); //将根元素植入文档doc中  
  18.   
  19.         number.setText("001");  
  20.   
  21.         name.setText("lnman");  
  22.   
  23.         age.setText("24");  
  24.   
  25.         student.addContent(number);  
  26.   
  27.         student.addContent(name);  
  28.   
  29.         student.addContent(age);  
  30.   
  31.         root.addContent(student);  
  32.   
  33.         Format format = Format.getCompactFormat();  
  34.   
  35.         format.setEncoding("gb2312"); //设置xml文件的字符为gb2312  
  36.   
  37.         format.setIndent("    "); //设置xml文件的缩进为4个空格  
  38.   
  39.         XMLOutputter XMLOut = new XMLOutputter(format);//元素后换行一层元素缩四格  
  40.   
  41.         XMLOut.output(doc, new FileOutputStream("studentinfo.xml"));   
  42.   
  43. }  
  44.   
  45.     public static void main(String[] args) throws Exception {  
  46.   
  47.         WriteXML w = new WriteXML();  
  48.   
  49.         System.out.println("Now we build an XML document .....");  
  50.   
  51.         w.BuildXML();  
  52.   
  53.         System.out.println("finished!");  
  54.   
  55. }  
  56.   
  57. }  

生成的xml文档为:

xml 代码
  1. <?xml version="1.0" encoding="gb2312"?>  
  2.   
  3. <student-info>  
  4.   
  5.     <student>  
  6.   
  7.         <number>001</number>  
  8.   
  9.         <name>lnman</name>  
  10.   
  11.         <age>24</age>  
  12.   
  13.     </student>  
  14.   
  15. </student-info>  

创建XML文档2

java 代码
 
  1. public class CreateXML {  
  2.   
  3.  public void Create() {  
  4.   
  5.   try {  
  6.   
  7.    Document doc = new Document();     
  8.   
  9.    ProcessingInstruction pi=new ProcessingInstruction("xml-stylesheet","type="text/xsl" href="test.xsl"");  
  10.   
  11.    doc.addContent(pi);     
  12.   
  13.    Namespace ns = Namespace.getNamespace("http://www.bromon.org" );  
  14.   
  15.    Namespace ns2 = Namespace.getNamespace("other""http://www.w3c.org" );  
  16.   
  17.    Element root = new Element("根元素", ns);  
  18.   
  19.    root.addNamespaceDeclaration(ns2);  
  20.   
  21.    doc.setRootElement(root);  
  22.   
  23.    Element el1 = new Element("元素一");  
  24.   
  25.    el1.setAttribute("属性""属性一");     
  26.   
  27.    Text text1=new Text("元素值");  
  28.   
  29.             Element em = new Element("元素二").addContent("第二个元素");  
  30.   
  31.    el1.addContent(text1);  
  32.   
  33.             el1.addContent(em);              
  34.   
  35.             Element el2 = new Element("元素三").addContent("第三个元素");  
  36.   
  37.             root.addContent(el1);  
  38.   
  39.             root.addContent(el2);              
  40.   
  41.             //缩进四个空格,自动换行,gb2312编码  
  42.   
  43.             XMLOutputter outputter = new XMLOutputter("  "true,"GB2312");  
  44.   
  45.             outputter.output(doc, new FileWriter("test.xml"));  
  46.   
  47.         }catch(Exception e)  {  
  48.   
  49.          System.out.println(e);  
  50.   
  51.         }  
  52.   
  53.     }      
  54.   
  55.     public static void main(String args[]) {  
  56.   
  57.      new CreateXML().Create();  
  58.   
  59.     }      
  60.   
  61. }  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值