一、XML簡介
xml是可擴展標記語言,主要用來標記數據、定義數據類型,非常適合萬維網傳輸。
xml特點:
xml是一種標記語言,很類似HTML xml的設計宗旨是傳輸數據,而不是顯示數據 xml標簽沒有被預定義,需要自行定義標簽 xml被設計為具有自我描述性 xml是W3C的推薦標准 xml和html對比: XML被設計為傳輸和存儲數據,其焦點是數據的內容。 html被設計用來顯示數據,其焦點是數據的外觀。 html旨在顯示信息,而xml旨在傳輸信息。 示例:元素可以包含其他元素、文本或者兩者的混合物。元素也可以擁有屬性。JK.Rowing 2005 35.2
Dom方式讀取xml文件:java Eclipse Swift Xcode C# Visual Studio
import java.io.File; import java.io.IOException; import java.util.Iterator; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class ReadXml { public static void main(String[] args) { try { // Dom方式 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("language.xml")); Element root = document.getDocumentElement(); System.out.println("cat=" + root.getAttribute("cat")); NodeList list = root.getElementsByTagName("lan"); for (int i = 0; i < list.getLength(); i++) { Element lan = (Element) list.item(i); System.out.println("------------------"); System.out.println("id=" + lan.getAttribute("id")); /* * Element name=(Element) * lan.getElementsByTagName("name").item(0); * System.out.println("name="+name.getTextContent()); Element * ide=(Element) lan.getElementsByTagName("ide").item(0); * System.out.println("ide="+ide.getTextContent()); */ NodeList clist = lan.getChildNodes(); for (int j = 0; j < clist.getLength(); j++) { Node c = clist.item(j); if (c instanceof Element) { System.out.println(c.getNodeName() + "=" + c.getTextContent()); } } } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
cat=it ------------------ id=1 name=java ide=Eclipse ------------------ id=2 name=Swift ide=Xcode ------------------ id=3 name=C# ide=Visual Studio三、Dom方式創建XML
package ucas.yp.xml; import java.io.File; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Result; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; public class CreateXml { public static void main(String[] args) { try { DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document=builder.newDocument(); Element root=document.createElement("Languages"); root.setAttribute("cat", "it"); Element lan1=document.createElement("lan"); lan1.setAttribute("id", "1"); Element name1=document.createElement("name"); name1.setTextContent("Java"); Element ide1=document.createElement("ide"); ide1.setTextContent("Eclipse"); lan1.appendChild(name1); lan1.appendChild(ide1); //從內到外一層層包裝 Element lan2=document.createElement("lan"); lan2.setAttribute("id", "2"); Element name2=document.createElement("name"); name2.setTextContent("Swift"); Element ide2=document.createElement("ide"); ide2.setTextContent("Xcode"); lan2.appendChild(name2); lan2.appendChild(ide2); Element lan3=document.createElement("lan"); lan3.setAttribute("id", "3"); Element name3=document.createElement("name"); name3.setTextContent("C#"); Element ide3=document.createElement("ide"); ide3.setTextContent("Visual Studio"); lan3.appendChild(name3); lan3.appendChild(ide3); root.appendChild(lan1); root.appendChild(lan2); root.appendChild(lan3); document.appendChild(root); TransformerFactory trFactory=TransformerFactory.newInstance(); Transformer transformer=trFactory.newTransformer(); StringWriter writer=new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer)); //----- 字符串輸出 System.out.println(writer.toString()); //-----生成xml文件 transformer.transform(new DOMSource(document), new StreamResult( new File("newxml.xml"))); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } } }四、使用Dom4j操作xml Dom4j是一個優秀的java操作xml的庫,具有性能優異、功能強大和極其易使用的特點。下載地址:http://www.dom4j.org/dom4j-1.6.1/
package ucas.yp.xml; import java.io.FileWriter; import java.io.IOException; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.io.XMLWriter; public class TestDom4j { public static void main(String[] args) { String xmlString = "刷新工程目錄,可以看到新生成的book.xml。"; try { Document document = DocumentHelper.parseText(xmlString); // 打印 System.out.println(document.asXML()); //生成xml文件 XMLWriter writer = new XMLWriter(new FileWriter("book.xml")); writer.write(document); writer.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } JK.Rowing 2005 35.2