Java 读写XML

  1 package dome4jTest;
  2 
  3 import java.io.FileWriter;
  4 import java.io.IOException;
  5 import java.net.URL;
  6 import java.util.Iterator;
  7 
  8 import org.dom4j.Attribute;
  9 import org.dom4j.Document;
 10 import org.dom4j.DocumentException;
 11 import org.dom4j.DocumentHelper;
 12 import org.dom4j.Element;
 13 import org.dom4j.io.OutputFormat;
 14 import org.dom4j.io.SAXReader;
 15 import org.dom4j.io.XMLWriter;
 16 
 17 /**
 18  * @author Young
 19  * @version 1.0
 20  *
 21  */
 22 public class Dom4j {
 23 
 24     private static String file = "output.xml";
 25     private static URL url;
 26 
 27     public static void main(String[] args) throws DocumentException, IOException {
 28         createDocument();
 29         
 30         url = new URL(
 31                 "http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=xml&ak=sXF8krbnX1LXbz0TpzivvOS2");
 32         
 33        
 34         SAXReader reader = new SAXReader();
 35 
 36         Document document = reader.read(url);
 37         
 38         Element root = document.getRootElement();
 39 //        System.out.println(root.getName());
 40 //         String arg0="results";
 41 //        String arg1="weather_data";
 42 //        System.out.println(root.element(arg0).element(arg1).element("date").getText());
 43 //        
 44         // iterate through child elements of root
 45         for (Iterator i = root.elementIterator(); i.hasNext();) {
 46             Element element = (Element) i.next();
 47             if(!element.getName().equals("results"))
 48             {
 49                 System.out.println("++++++++++++++++++++++++++");
 50                 continue;
 51             }
 52             
 53             for(Iterator k = element.elementIterator(); k.hasNext();)
 54             {
 55                 Element childElement = (Element) k.next();
 56                 if(!childElement.getName().equals("weather_data"))
 57                 {
 58                     System.out.println("==============================");
 59                     continue;
 60                 }
 61 //                System.out.println("get Name -->" + childElement.getName());
 62 //                System.out.println("get type -->" + childElement.getData());
 63 //                System.out.println("get type -->" + childElement.getText());
 64                 
 65                 for(Iterator m = element.elementIterator(); m.hasNext();)
 66                 {
 67                     Element grandElement = (Element) m.next();
 68                     System.out.println("get Name -->" + grandElement.getName());
 69                     System.out.println("get Path-->" + grandElement.getPath());
 70                     System.out.println("get Text -->" + grandElement.getText());
 71                     System.out.println("get nodeCount -->" + grandElement.nodeCount());
 72                     System.out.println("get Node Type-->" + grandElement.getNodeType());
 73                     System.out.println("get Node Type-->" + grandElement.getNodeTypeName());
 74                     
 75                 }
 76                 
 77             }
 78          
 79             // iterate through attributes of root
 80             for (Iterator j = element.attributeIterator(); j.hasNext();) {
 81                 Attribute attribute = (Attribute) j.next();
 82                 System.out.println("attribute name " + element.getName() + " ->" + attribute.getName());
 83                 System.out.println("attribute name " + element.getName() + " ->" + attribute.getPath());
 84                 
 85             }
 86         }
 87 
 88         // iterate through child elements of root with element name "foo"
 89         for (Iterator i = root.elementIterator("results"); i.hasNext();) {
 90             Element results = (Element) i.next();
 91             System.out.println("results -->" + results.getName());
 92             System.out.println("results -->" + results.getData());
 93             // iterate through attributes of root
 94             for (Iterator j = results.attributeIterator(); j.hasNext();) {
 95                 Attribute attribute = (Attribute) j.next();
 96                 System.out.println("attribute name " + results.getName() + " ->" + attribute.getName());
 97                 System.out.println("attribute name " + results.getName() + " ->" + attribute.getPath());
 98                 
 99             }
100         }
101 
102     }
103 
104     public static Document createDocument() throws IOException {
105         OutputFormat format = OutputFormat.createPrettyPrint();
106         XMLWriter writer = new XMLWriter(new FileWriter("output.xml"), format);
107 
108         Document document = DocumentHelper.createDocument();
109         Element root = document.addElement("root");
110 
111         root.addAttribute("ClassA", "Test2");
112         root.addAttribute("ClassB", "Test2");
113         Element author1 = root.addElement("author").addAttribute("name", "James").addAttribute("location", "UK")
114                 .addText("James Strachan").addAttribute("Test1", "Values").addText("Test");
115 
116         Element author2 = root.addElement("author").addAttribute("name", "Bob").addAttribute("location", "US")
117                 .addText("Bob McWhirter");
118 
119         Element author3 = root.addElement("author").addAttribute("name", "Jack").addAttribute("location", "US")
120                 .addText("Jack Ma");
121 
122         writer.write(document);
123         writer.close();
124         return document;
125     }
126 
127 }

 

import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * @author Young
 * @version 1.0
 *
 */
public class Dom4j {

    private static String file = "output.xml";
    private static URL url;

    public static void main(String[] args) throws DocumentException,
            IOException {
        createDocument();

        url = new URL(
                "http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=xml&ak=sXF8krbnX1LXbz0TpzivvOS2");

        SAXReader reader = new SAXReader();

        Document document = reader.read(url);

        Element root = document.getRootElement();
        // System.out.println(root.getName());
        // String arg0="results";
        // String arg1="weather_data";
        // System.out.println(root.element(arg0).element(arg1).element("date").getText());
        //
        // iterate through child elements of root
        for (Iterator i = root.elementIterator(); i.hasNext();) {
            Element element = (Element) i.next();
            if (!element.getName().equals("results")) {
                System.out.println("++++++++++++++++++++++++++");
                continue;
            }

            for (Iterator k = element.elementIterator(); k.hasNext();) {
                Element childElement = (Element) k.next();
                if (!childElement.getName().equals("weather_data")) {
                    System.out.println("==============================");
                    continue;
                }
                // System.out.println("get Name -->" + childElement.getName());
                // System.out.println("get type -->" + childElement.getData());
                // System.out.println("get type -->" + childElement.getText());

                for (Iterator m = element.elementIterator(); m.hasNext();) {
                    Element grandElement = (Element) m.next();
                    System.out.println("get Name -->" + grandElement.getName());
                    System.out.println("get Path-->" + grandElement.getPath());
                    System.out.println("get Text -->" + grandElement.getText());
                    System.out.println("get nodeCount -->"
                            + grandElement.nodeCount());
                    System.out.println("get Node Type-->"
                            + grandElement.getNodeType());
                    System.out.println("get Node Type-->"
                            + grandElement.getNodeTypeName());

                }

            }

            // iterate through attributes of root
            for (Iterator j = element.attributeIterator(); j.hasNext();) {
                Attribute attribute = (Attribute) j.next();
                System.out.println("attribute name " + element.getName()
                        + " ->" + attribute.getName());
                System.out.println("attribute name " + element.getName()
                        + " ->" + attribute.getPath());

            }
        }

        // iterate through child elements of root with element name "foo"
        for (Iterator i = root.elementIterator("results"); i.hasNext();) {
            Element results = (Element) i.next();
            System.out.println("results -->" + results.getName());
            System.out.println("results -->" + results.getData());
            // iterate through attributes of root
            for (Iterator j = results.attributeIterator(); j.hasNext();) {
                Attribute attribute = (Attribute) j.next();
                System.out.println("attribute name " + results.getName()
                        + " ->" + attribute.getName());
                System.out.println("attribute name " + results.getName()
                        + " ->" + attribute.getPath());

            }
        }

    }

    public static Document createDocument() throws IOException {
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(new FileWriter("output.xml"), format);

        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("note");

        root.addAttribute("ClassA", "Test2");
        root.addAttribute("ClassB", "Test2");
        root.addComment("This is a comment");
        Element author1 = root.addElement("author")
                .addAttribute("name", "James").addAttribute("location", "UK")
                .addText("James Strachan").addAttribute("Test1", "Values")
                .addText("Test").addElement("Test").addAttribute("A", "B").addText("This is Test");

        Element author2 = root.addElement("author").addAttribute("name", "Bob")
                .addAttribute("location", "US").addText("Bob McWhirter");

        Element author3 = root.addElement("author")
                .addAttribute("name", "Jack").addAttribute("location", "US")
                .addText("Jack Ma");

        writer.write(document);
        writer.close();
        return document;
    }

}

 

你可能感兴趣的:(java)