DOM解析的例子 – JAVA編程語言程序開發技術文章

下面代碼使用DOM解析XML文件

[java]
package com.app; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.NodeList; 
 
public class DOMXMLReader { 
 
    public static void main(String args[]) { 
        try { 
            DocumentBuilderFactory factory = DocumentBuilderFactory 
                    .newInstance(); 
            DocumentBuilder builder = factory.newDocumentBuilder(); 
            Document doc = builder.parse("url.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(); 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 

下面代碼修改XML文件: 
package com.app; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.transform.Transformer; 
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; 
import org.w3c.dom.Text; 
 
public class DOMXMLWriter { 
 
    public static void main(String args[]) { 
        try { 
            DocumentBuilderFactory factory = DocumentBuilderFactory 
                    .newInstance(); 
            DocumentBuilder builder = factory.newDocumentBuilder(); 
            Document doc = builder.parse("url.xml"); 
            doc.normalize(); 
            // —取得變量—-  
            String text = "itjob"; 
            String url = "www.5itjob.com"; 
            String author = "xsw"; 
            String discription = "a good website for it job!"; 
            // ————-  
            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("url.xml")); 
            transformer.transform(source, result); 
            System.out.println("write complete!"); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 

 

摘自  Java教程
 

發佈留言