jdom 组装xml以及解析xml

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

    package com.test;  
      
    import java.io.FileOutputStream;  
    import java.util.List;  
      
    import org.jdom2.Document;  
    import org.jdom2.Element;  
    import org.jdom2.input.SAXBuilder;  
    import org.jdom2.output.Format;  
    import org.jdom2.output.XMLOutputter;  
      
    /** 
     * @说明 JDom生成解析XML 
     * @author cuisuqiang 
     * @version 1.0 
     * @since 
     */  
    @SuppressWarnings("unchecked")  
    public class JDomDemo {  
        public static void main(String[] args) {  
            String file = "C:\\p.xml"; // 文件存放位置  
            JDomDemo dj = new JDomDemo();  
            dj.createXml(file);  
            dj.parserXml(file);  
        }  

        /**   
         * 生成XML   
         * @param filePath 文件路径   
         */  
        public void createXml(String fileName) {  
            Element root = new Element("persons");  
            Document document = new Document(root);  
            Element person = new Element("person");  
            root.addContent(person);  
            Element name = new Element("name");  
            name.setText("java小强");  
            person.addContent(name);  
            Element sex = new Element("sex");  
            sex.setText("man");  
            person.addContent(sex);  
            Element age = new Element("age");  
            age.setText("23");  
            person.addContent(age);  
            XMLOutputter XMLOut = new XMLOutputter();  
            try {  
                Format f = Format.getPrettyFormat();  
                f.setEncoding("UTF-8");//default=UTF-8  
                XMLOut.setFormat(f);  
                XMLOut.output(document, new FileOutputStream(fileName));  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
         /**   
         * 解析XML   
         * @param filePath 文件路径   
         */   
        public void parserXml(String fileName) {  
            try {  
                SAXBuilder builder = new SAXBuilder();  
                Document document = builder.build(fileName);  
                Element root = document.getRootElement();  
                List persons = root.getChildren("person");  
                for (int i = 0; i < persons.size(); i++) {  
                    Element person = (Element) persons.get(i);  
                    List pros = person.getChildren();  
                    for (int j = 0; j < pros.size(); j++) {  
                        Element element = (Element) pros.get(j);  
                        System.out.println(element.getName() + ":" + element.getValue());  
                    }  
                }  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    }  

 


来自:http://cuisuqiang.iteye.com/ ! 

在上面我们看到在解析xml的时候使用的是filepath 也就是本地的xml 文件,但是我们有时会访问接口通过接口来获取xml文件这时怎么办那其实不管是filepath 还是inputStream还是url其实都是一样,因为不管哪种方式都要将其从 inputStream 流转化成String 在进行解析,其实filePath  和url 都是一样的一个来自本地一个来自网络:

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

所需要的jar包以及api文档可以到官网下载:http://www.jdom.org/

这里也有api文档可以下载:jdom api文档





你可能感兴趣的:(jdom 组装xml以及解析xml)