xml的写:
code:
1 import java.io.File; 2 import java.io.StringWriter; 3 4 import javax.xml.parsers.DocumentBuilder; 5 import javax.xml.parsers.DocumentBuilderFactory; 6 import javax.xml.parsers.ParserConfigurationException; 7 import javax.xml.transform.Transformer; 8 import javax.xml.transform.TransformerConfigurationException; 9 import javax.xml.transform.TransformerException; 10 import javax.xml.transform.TransformerFactory; 11 import javax.xml.transform.dom.DOMSource; 12 import javax.xml.transform.stream.StreamResult; 13 14 import org.w3c.dom.Document; 15 import org.w3c.dom.Element; 16 17 public class WriteXML { 18 19 public static void main(String[] args) { 20 21 try { 22 23 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 24 DocumentBuilder builder = factory.newDocumentBuilder(); 25 Document document = builder.newDocument(); 26 Element root = document.createElement("Languages"); 27 root.setAttribute("cat", "it"); 28 29 Element lan1 = document.createElement("lan"); 30 lan1.setAttribute("id", "1"); 31 Element name1 = document.createElement("name"); 32 Element ide1 = document.createElement("ide"); 33 name1.setTextContent("Java"); 34 ide1.setTextContent("eclipse"); 35 lan1.appendChild(name1); 36 lan1.appendChild(ide1); 37 38 Element lan2 = document.createElement("lan"); 39 lan1.setAttribute("id", "2"); 40 Element name2 = document.createElement("name"); 41 Element ide2 = document.createElement("ide"); 42 name2.setTextContent("Swift"); 43 ide2.setTextContent("Xcode"); 44 lan2.appendChild(name2); 45 lan2.appendChild(ide2); 46 47 Element lan3 = document.createElement("lan"); 48 lan3.setAttribute("id", "3"); 49 Element name3 = document.createElement("name"); 50 Element ide3 = document.createElement("ide"); 51 name3.setTextContent("C#"); 52 ide3.setTextContent("Visual Studio"); 53 lan3.appendChild(name3); 54 lan3.appendChild(ide3); 55 56 root.appendChild(lan1); 57 root.appendChild(lan2); 58 root.appendChild(lan3); 59 document.appendChild(root); 60 61 //----------------------- 62 63 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 64 Transformer tansformer = transformerFactory.newTransformer(); 65 StringWriter writer = new StringWriter(); 66 tansformer.transform(new DOMSource(document), new StreamResult(writer)); 67 System.out.println(writer.toString());//输出到控制台 68 69 70 tansformer.transform(new DOMSource(document), new StreamResult(new File("newxml.xml")));//输出到xml文本 71 } catch (ParserConfigurationException e) { 72 73 e.printStackTrace(); 74 } catch (TransformerConfigurationException e) { 75 e.printStackTrace(); 76 } catch (TransformerException e) { 77 78 e.printStackTrace(); 79 } 80 } 81 82 }
xml的读:
code:
1 import java.io.File; 2 import java.io.IOException; 3 4 import javax.xml.parsers.DocumentBuilder; 5 import javax.xml.parsers.DocumentBuilderFactory; 6 import javax.xml.parsers.ParserConfigurationException; 7 8 import org.w3c.dom.Document; 9 import org.w3c.dom.Element; 10 import org.w3c.dom.Node; 11 import org.w3c.dom.NodeList; 12 import org.xml.sax.SAXException; 13 14 public class TestReadXML { 15 16 public static void main(String[] args) { 17 //DOM 18 try { 19 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 20 DocumentBuilder builder = factory.newDocumentBuilder(); 21 Document document = builder.parse(new File("languages.xml")); 22 Element root = document.getDocumentElement();//获取根元素 23 System.out.println("cat="+root.getAttribute("cat")); 24 NodeList list = root.getElementsByTagName("lan");//获取子元素lan 25 for(int i=0;i<list.getLength();i++){ 26 Element lan = (Element) list.item(i); 27 System.out.println("--------"); 28 System.out.println("id="+lan.getAttribute("id")); 29 30 // Element name = (Element) lan.getElementsByTagName("name").item(0);//不使用这种方法 31 // System.out.println("name="+name.getTextContent()); 32 33 NodeList clist = lan.getChildNodes();//获取lan的所有子元素 34 for(int j=0;j<clist.getLength();j++){ 35 Node c = clist.item(j); 36 if(c instanceof Element) 37 System.out.println(c.getNodeName()+"="+c.getTextContent()); 38 } 39 } 40 41 } catch (ParserConfigurationException e) { 42 43 e.printStackTrace(); 44 } catch (SAXException e) { 45 46 e.printStackTrace(); 47 } catch (IOException e) { 48 49 e.printStackTrace(); 50 } 51 } 52 53 }