关闭

DOM解析的例子:

标签: stringexceptionurldatewebsiteclass
318人阅读 评论(0) 收藏 举报
分类:

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

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();
		}
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:126204次
    • 积分:3613
    • 等级:
    • 排名:第9043名
    • 原创:256篇
    • 转载:0篇
    • 译文:0篇
    • 评论:7条
    文章均来自以下教程网
    文章分类
    文章存档
    最新评论