DOM解析的例子:

原创 2012年03月27日 09:34:27

下面代码使用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();
		}
	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

dom4j和jdom解析XML小例子

采用DOM4J解析xml,带例子

这里主要是解析一般常用的三级,更多级数可以写死,一层一层的累加,也可以写个灵活的方法来判断级数在解析。例子适合才接触的新人。 解析的xml: [html] view plainco...

dom4j 解析xml文件小例子

  • 2011-09-08 00:37
  • 283KB
  • 下载

Java DOM解析XML的几个例子

Sample1: 1:新建XML文档 books.xml,放到项目的根目录下。 Thinking in JAVA ...

用dom4j解析xml小例子

  • 2009-02-10 16:25
  • 467KB
  • 下载

Dom4jXML解析教程例子

  • 2009-10-10 13:19
  • 929KB
  • 下载

在java中使用dom4j解析xmls的简单例子

虽然Java中已经有了Dom和Sax这两种标准解析方式 但其操作起来并不轻松,对于我这么一个初学者来说,其中部分代码是活生生的恶心 为此,伟大的第三方开发组开发出了Jdom和Dom4j等工具 鉴...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)