JAXP---
/**
* 根据文件路径建立DOM Document对象。
*
*@param name 文件名
*@return org.w3c.dom.Document
*@exception Exception 打开文件出错时抛出
*/
public final static org.w3c.dom.Document getDocument( String name )
throws Exception
{
String uri = "file:" + new File( name ).getAbsolutePath(); // 使用JAXP作为解析器
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document document = db.parse( uri );
return document;
}
/**
* 根据文件路径建立DOM Document对象。
*
*@param name 文件名
*@return org.w3c.dom.Document
*@exception Exception 打开文件出错时抛出
*/
public final static org.w3c.dom.Document getDocument( String name )
throws Exception
{
String uri = "file:" + new File( name ).getAbsolutePath(); // 使用JAXP作为解析器
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document document = db.parse( uri );
return document;
}
解决方案 »
- MyEclipse安装ADT后丢失其它功能
- jxl导入判断单元格是否合并的问题
- 为什么struts的ActionForm不执行set
- 急求高手帮我解决下问题,实在是没办法了,想了4天多了,使用Increment生成器
- weblogic9.1下用jstl1.1报错
- Hibernate问题~~~session.beginTransaction()出错~~~~
- 我初用netBean5.5,出现个问题
- !!!请英雄提供一个完整可靠的开源连接池实现!!!
- 開發EJB,JSP現在流行用什麼工具啊?
- java程序如何通过socket程序传送文件和数据?
- 急问jbuilder6.0下的javax.ejb包哪里去了?
- 哪位有用Servlet通过DOM处理Xml的例子?
/**
* 取得对应指定名称文件的DOM文档对象
*
*@param name XML文件名称。
*@return org.jdom.Document
*@throws java.io.IOException
*@throws org.jdom.JDOMException
*/
public org.jdom.Document getJDOMDocument( java.lang.String name )
throws java.io.IOException,
org.jdom.JDOMException
{
String uri = "file:" + new File( name ).getAbsolutePath(); // 取文档对象
//org.jdom.input.DOMBuilder builder = new org.jdom.input.DOMBuilder();
org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder();
org.jdom.Document document = builder.build( new File( name ) ); return document;
}
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.InputSource;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Text;Document doc = null;
Element root = null;
IputSource xmlInp = new InputSource(urlStr.openStream());DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
doc = parser.parse(xmlInp);
//取得XNL文档的根element
root = doc.getDocumentElement();
root.normalize();
//下面就解析文档
取得Node的类型和属性值
我看各位楼上的大哥用的都是固定XML文档的处理方式,如果XML文档是动态生成的呢?那就没有了文件名啊,怎么导入??
2、解析时不使用db.parse( uri );,用db.parse( xmlString);
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
public class xmldisplay {
public static void main(String args[]){
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse(new File("d:/20025241111.xml"));
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();
}}
}links.xml源码:
<?xml version="1.0" encoding="GBK"?>
<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>
Document doc=builder.parse(new File("d:/20025241111.xml"));
改为
Document doc=builder.parse(new File("d:/links.xml"));