jdom 组装xml以及解析xml

[java]  view plain  copy
  1. 到官方网站下载JDOM包http://www.jdom.org/   
[java]  view plain  copy
  1. 注意的是,版本1和版本2的类路径已经变更,如果你是更新使用版本2,则需要重新编译你的代码  


[java]  view plain  copy
  1. package com.test;    
  2.     
  3. import java.io.FileOutputStream;    
  4. import java.util.List;    
  5.     
  6. import org.jdom2.Document;    
  7. import org.jdom2.Element;    
  8. import org.jdom2.input.SAXBuilder;    
  9. import org.jdom2.output.Format;    
  10. import org.jdom2.output.XMLOutputter;    
  11.     
  12. /**  
  13.  * @说明 JDom生成解析XML  
  14.  * @author cuisuqiang  
  15.  * @version 1.0  
  16.  * @since  
  17.  */    
  18. @SuppressWarnings("unchecked")    
  19. public class JDomDemo {    
  20.     public static void main(String[] args) {    
  21.         String file = "C:\\p.xml"// 文件存放位置    
  22.         JDomDemo dj = new JDomDemo();    
  23.         dj.createXml(file);    
  24.         dj.parserXml(file);    
  25.     }    
  26.   
  27.     /**    
  28.      * 生成XML    
  29.      * @param filePath 文件路径    
  30.      */    
  31.     public void createXml(String fileName) {    
  32.         Element root = new Element("persons");    
  33.         Document document = new Document(root);    
  34.         Element person = new Element("person");    
  35.         root.addContent(person);    
  36.         Element name = new Element("name");    
  37.         name.setText("java小强");    
  38.         person.addContent(name);    
  39.         Element sex = new Element("sex");    
  40.         sex.setText("man");    
  41.         person.addContent(sex);    
  42.         Element age = new Element("age");    
  43.         age.setText("23");    
  44.         person.addContent(age);    
  45.         XMLOutputter XMLOut = new XMLOutputter();    
  46.         try {    
  47.             Format f = Format.getPrettyFormat();    
  48.             f.setEncoding("UTF-8");//default=UTF-8    
  49.             XMLOut.setFormat(f);    
  50.             XMLOut.output(document, new FileOutputStream(fileName));    
  51.         } catch (Exception e) {    
  52.             e.printStackTrace();    
  53.         }    
  54.     }    
  55.      /**    
  56.      * 解析XML    
  57.      * @param filePath 文件路径    
  58.      */     
  59.     public void parserXml(String fileName) {    
  60.         try {    
  61.             SAXBuilder builder = new SAXBuilder();    
  62.             Document document = builder.build(fileName);    
  63.             Element root = document.getRootElement();    
  64.             List persons = root.getChildren("person");    
  65.             for (int i = 0; i < persons.size(); i++) {    
  66.                 Element person = (Element) persons.get(i);    
  67.                 List pros = person.getChildren();    
  68.                 for (int j = 0; j < pros.size(); j++) {    
  69.                     Element element = (Element) pros.get(j);    
  70.                     System.out.println(element.getName() + ":" + element.getValue());    
  71.                 }    
  72.             }    
  73.         } catch (Exception e) {    
  74.             e.printStackTrace();    
  75.         }    
  76.     }    
  77. }    
  78.   
  79.   
  80.   
  81.   
  82. ttp://cuisuqiang.iteye.com/ !   
在上面我们看到在解析xml的时候使用的是filepath 也就是本地的xml 文件,但是我们有时会访问接口通过接口来获取xml文件这时怎么办那其实不管是filepath 还是inputStream还是url其实都是一样,因为不管哪种方式都要将其从 inputStream 流转化成String 在进行解析,其实filePath  和url 都是一样的一个来自本地一个来自网络:

[java]  view plain  copy
  1. SAXBuilder builder = new SAXBuilder();    
[java]  view plain  copy
  1. builder.build(URL url);   
  2. 其实SAXBuilder类的build()方法有很多重载,其中可以传File InputStream Reader  URL 等参数具体的有哪些方法可以参考jdom  api   

[java]  view plain  copy
  1. 到官方网站下载JDOM包http://www.jdom.org/   
[java]  view plain  copy
  1. 注意的是,版本1和版本2的类路径已经变更,如果你是更新使用版本2,则需要重新编译你的代码  

[java]  view plain  copy
  1. package com.test;    
  2.     
  3. import java.io.FileOutputStream;    
  4. import java.util.List;    
  5.     
  6. import org.jdom2.Document;    
  7. import org.jdom2.Element;    
  8. import org.jdom2.input.SAXBuilder;    
  9. import org.jdom2.output.Format;    
  10. import org.jdom2.output.XMLOutputter;    
  11.     
  12. /**  
  13.  * @说明 JDom生成解析XML  
  14.  * @author cuisuqiang  
  15.  * @version 1.0  
  16.  * @since  
  17.  */    
  18. @SuppressWarnings("unchecked")    
  19. public class JDomDemo {    
  20.     public static void main(String[] args) {    
  21.         String file = "C:\\p.xml"// 文件存放位置    
  22.         JDomDemo dj = new JDomDemo();    
  23.         dj.createXml(file);    
  24.         dj.parserXml(file);    
  25.     }    
  26.   
  27.     /**    
  28.      * 生成XML    
  29.      * @param filePath 文件路径    
  30.      */    
  31.     public void createXml(String fileName) {    
  32.         Element root = new Element("persons");    
  33.         Document document = new Document(root);    
  34.         Element person = new Element("person");    
  35.         root.addContent(person);    
  36.         Element name = new Element("name");    
  37.         name.setText("java小强");    
  38.         person.addContent(name);    
  39.         Element sex = new Element("sex");    
  40.         sex.setText("man");    
  41.         person.addContent(sex);    
  42.         Element age = new Element("age");    
  43.         age.setText("23");    
  44.         person.addContent(age);    
  45.         XMLOutputter XMLOut = new XMLOutputter();    
  46.         try {    
  47.             Format f = Format.getPrettyFormat();    
  48.             f.setEncoding("UTF-8");//default=UTF-8    
  49.             XMLOut.setFormat(f);    
  50.             XMLOut.output(document, new FileOutputStream(fileName));    
  51.         } catch (Exception e) {    
  52.             e.printStackTrace();    
  53.         }    
  54.     }    
  55.      /**    
  56.      * 解析XML    
  57.      * @param filePath 文件路径    
  58.      */     
  59.     public void parserXml(String fileName) {    
  60.         try {    
  61.             SAXBuilder builder = new SAXBuilder();    
  62.             Document document = builder.build(fileName);    
  63.             Element root = document.getRootElement();    
  64.             List persons = root.getChildren("person");    
  65.             for (int i = 0; i < persons.size(); i++) {    
  66.                 Element person = (Element) persons.get(i);    
  67.                 List pros = person.getChildren();    
  68.                 for (int j = 0; j < pros.size(); j++) {    
  69.                     Element element = (Element) pros.get(j);    
  70.                     System.out.println(element.getName() + ":" + element.getValue());    
  71.                 }    
  72.             }    
  73.         } catch (Exception e) {    
  74.             e.printStackTrace();    
  75.         }    
  76.     }    
  77. }    
  78.   
  79.   
  80.   
  81.   
  82. ttp://cuisuqiang.iteye.com/ !   
在上面我们看到在解析xml的时候使用的是filepath 也就是本地的xml 文件,但是我们有时会访问接口通过接口来获取xml文件这时怎么办那其实不管是filepath 还是inputStream还是url其实都是一样,因为不管哪种方式都要将其从 inputStream 流转化成String 在进行解析,其实filePath  和url 都是一样的一个来自本地一个来自网络:

[java]  view plain  copy
  1. 到官方网站下载JDOM包http://www.jdom.org/   
[java]  view plain  copy
  1. 注意的是,版本1和版本2的类路径已经变更,如果你是更新使用版本2,则需要重新编译你的代码  

[java]  view plain  copy
  1. package com.test;    
  2.     
  3. import java.io.FileOutputStream;    
  4. import java.util.List;    
  5.     
  6. import org.jdom2.Document;    
  7. import org.jdom2.Element;    
  8. import org.jdom2.input.SAXBuilder;    
  9. import org.jdom2.output.Format;    
  10. import org.jdom2.output.XMLOutputter;    
  11.     
  12. /**  
  13.  * @说明 JDom生成解析XML  
  14.  * @author cuisuqiang  
  15.  * @version 1.0  
  16.  * @since  
  17.  */    
  18. @SuppressWarnings("unchecked")    
  19. public class JDomDemo {    
  20.     public static void main(String[] args) {    
  21.         String file = "C:\\p.xml"// 文件存放位置    
  22.         JDomDemo dj = new JDomDemo();    
  23.         dj.createXml(file);    
  24.         dj.parserXml(file);    
  25.     }    
  26.   
  27.     /**    
  28.      * 生成XML    
  29.      * @param filePath 文件路径    
  30.      */    
  31.     public void createXml(String fileName) {    
  32.         Element root = new Element("persons");    
  33.         Document document = new Document(root);    
  34.         Element person = new Element("person");    
  35.         root.addContent(person);    
  36.         Element name = new Element("name");    
  37.         name.setText("java小强");    
  38.         person.addContent(name);    
  39.         Element sex = new Element("sex");    
  40.         sex.setText("man");    
  41.         person.addContent(sex);    
  42.         Element age = new Element("age");    
  43.         age.setText("23");    
  44.         person.addContent(age);    
  45.         XMLOutputter XMLOut = new XMLOutputter();    
  46.         try {    
  47.             Format f = Format.getPrettyFormat();    
  48.             f.setEncoding("UTF-8");//default=UTF-8    
  49.             XMLOut.setFormat(f);    
  50.             XMLOut.output(document, new FileOutputStream(fileName));    
  51.         } catch (Exception e) {    
  52.             e.printStackTrace();    
  53.         }    
  54.     }    
  55.      /**    
  56.      * 解析XML    
  57.      * @param filePath 文件路径    
  58.      */     
  59.     public void parserXml(String fileName) {    
  60.         try {    
  61.             SAXBuilder builder = new SAXBuilder();    
  62.             Document document = builder.build(fileName);    
  63.             Element root = document.getRootElement();    
  64.             List persons = root.getChildren("person");    
  65.             for (int i = 0; i < persons.size(); i++) {    
  66.                 Element person = (Element) persons.get(i);    
  67.                 List pros = person.getChildren();    
  68.                 for (int j = 0; j < pros.size(); j++) {    
  69.                     Element element = (Element) pros.get(j);    
  70.                     System.out.println(element.getName() + ":" + element.getValue());    
  71.                 }    
  72.             }    
  73.         } catch (Exception e) {    
  74.             e.printStackTrace();    
  75.         }    
  76.     }    
  77. }    
  78.   
  79.   
  80.   
  81.   
  82. ttp://cuisuqiang.iteye.com/ !   
在上面我们看到在解析xml的时候使用的是filepath 也就是本地的xml 文件,但是我们有时会访问接口通过接口来获取xml文件这时怎么办那其实不管是filepath 还是inputStream还是url其实都是一样,因为不管哪种方式都要将其从 inputStream 流转化成String 在进行解析,其实filePath  和url 都是一样的一个来自本地一个来自网络:

[java]  view plain  copy
  1. SAXBuilder builder = new SAXBuilder();    
[java]  view plain  copy
  1. builder.build(URL url);   
  2. 其实SAXBuilder类的build()方法有很多重载,其中可以传File InputStream Reader  URL 等参数具体的有哪些方法可以参考jdom  api   



[java]  view plain  copy
  1. SAXBuilder builder = new SAXBuilder();    
[java]  view plain  copy
  1. builder.build(URL url);   
  2. 其实SAXBuilder类的build()方法有很多重载,其中可以传File InputStream Reader  URL 等参数具体的有哪些方法可以参考jdom  api   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值