| 1.基本的DOM对象 |

| 2.DOM实例 |
<?xml version="1.0" standalone="yes"?> <links> <link> <text>JSP Insider</text> <url newWindow="no">http://www.jspinsider.com</url> <author>JSP Insider</author> <date> <day>2</day> <month>1</month> <year>2001</year> </date> <description>A JSP information site.</description> </link> <link> <text>The makers of Java</text> <url newWindow="no">http://java.sun.com</url> <author>Sun Microsystems</author> <date> <day>3</day> <month>1</month> <year>2001</year> </date> <description>Sun Microsystem's website.</description> </link> <link> <text>The standard JSP container</text> <url newWindow="no">http://jakarta.apache.org</url> <author>Apache Group</author> <date> <day>4</day> <month>1</month> <year>2001</year> </date> <description>Some great software.</description> </link> </links>
| import javax.xml.parsers.*; import org.w3c.dom.*; |
| DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document doc=builder.parse("links.xml"); doc.normalize(); |
| NodeList links =doc.getElementsByTagName("link"); |
for (int i=0;i<links.getLength();i++){ Element link=(Element) links.item(i); System.out.print("Content: "); System.out.println(link.getElementsByTagName("text").item(0).getFirstChild().getNodeValue()); System.out.print("URL: "); System.out.println(link.getElementsByTagName("url").item(0).getFirstChild().getNodeValue()); System.out.print("Author: "); System.out.println(link.getElementsByTagName("author").item(0).getFirstChild().getNodeValue()); System.out.print("Date: "); Element linkdate=(Element) link.getElementsByTagName("date").item(0); String day=linkdate.getElementsByTagName("day").item(0).getFirstChild().getNodeValue(); String month=linkdate.getElementsByTagName("month").item(0).getFirstChild().getNodeValue(); String year=linkdate.getElementsByTagName("year").item(0).getFirstChild().getNodeValue(); System.out.println(day+"-"+month+"-"+year); System.out.print("Description: "); System.out.println(link.getElementsByTagName("description").item(0).getFirstChild().getNodeValue()); System.out.println(); }
| import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.*; |
| DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document doc=builder.parse("links.xml"); doc.normalize(); //---取得变量---- String text="Hanzhong's Homepage"; String url="www.hzliu.com"; String author="Hzliu Liu"; String discription="A site from Hanzhong Liu, give u lots of suprise!!!"; |
| Text textseg; Element link=doc.createElement("link"); |
| Element linktext=doc.createElement("text"); textseg=doc.createTextNode(text); linktext.appendChild(textseg); link.appendChild(linktext); Element linkurl=doc.createElement("url"); textseg=doc.createTextNode(url); linkurl.appendChild(textseg); link.appendChild(linkurl); Element linkauthor=doc.createElement("author"); textseg=doc.createTextNode(author); linkauthor.appendChild(textseg); link.appendChild(linkauthor); java.util.Calendar rightNow = java.util.Calendar.getInstance(); String day=Integer.toString(rightNow.get(java.util.Calendar.DAY_OF_MONTH)); String month=Integer.toString(rightNow.get(java.util.Calendar.MONTH)); String year=Integer.toString(rightNow.get(java.util.Calendar.YEAR)); Element linkdate=doc.createElement("date"); Element linkdateday=doc.createElement("day"); textseg=doc.createTextNode(day); linkdateday.appendChild(textseg); Element linkdatemonth=doc.createElement("month"); textseg=doc.createTextNode(month); linkdatemonth.appendChild(textseg); Element linkdateyear=doc.createElement("year"); textseg=doc.createTextNode(year); linkdateyear.appendChild(textseg); linkdate.appendChild(linkdateday); linkdate.appendChild(linkdatemonth); linkdate.appendChild(linkdateyear); link.appendChild(linkdate); Element linkdiscription=doc.createElement("description"); textseg=doc.createTextNode(discription); linkdiscription.appendChild(textseg); link.appendChild(linkdiscription); |
| doc.getDocumentElement().appendChild(link); |
| TransformerFactory tFactory =TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new java.io.File("links.xml")); transformer.transform(source, result); |