当前位置: 技术问答>java相关
请问sun有没有提供一个直接能把整个xml文件中的所有元素都读出的方法????
来源: 互联网 发布时间:2015-03-24
本文导语: 每次都要编程遍历整个dom树,太麻烦了! | 你可以用jaxp API ,先到sun网站上下载 .jar文件。 csdn首页不是有一个名为“Sun推出 Java XML Pack 2001年冬季版 ”的新闻吗?去sun把它下载下来,然...
每次都要编程遍历整个dom树,太麻烦了!
|
你可以用jaxp API ,先到sun网站上下载 .jar文件。
csdn首页不是有一个名为“Sun推出 Java XML Pack 2001年冬季版 ”的新闻吗?去sun把它下载下来,然后 在你的java classpath中包含xalan.jar和xerces.jar两个jar文件。
然后试一下下面的程序
import java.io.*;
import java.util.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException ;
import org.xml.sax.SAXParseException;
import java.io.IOException;
import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
//..........
//...........
Document document;
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
//factory.setValidating(true);
//factory.setNamespaceAware(true);
try {
String realpathname=request.getRealPath("/xml/menu.xml");
File f = new File(realpathname);
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(f);
// Use a Transformer for output
TransformerFactory tFactory =
TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
}
catch (TransformerConfigurationException tce) {
// Error generated by the parser
System.out.println ("n** Transformer Factory error");
System.out.println(" " + tce.getMessage() );
// Use the contained exception, if any
Throwable x = tce;
if (tce.getException() != null)
x = tce.getException();
x.printStackTrace();
} catch (TransformerException te) {
// Error generated by the parser
System.out.println ("n** Transformation error");
System.out.println(" " + te.getMessage() );
// Use the contained exception, if any
Throwable x = te;
if (te.getException() != null)
x = te.getException();
x.printStackTrace();
}
catch (SAXException sxe) {
// Error generated during parsing)
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace();
} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
pce.printStackTrace();
} catch (IOException ioe)
{
// I/O error
ioe.printStackTrace();
}
此程序将 xml文件读出来,然后 system.out显示出来。
csdn首页不是有一个名为“Sun推出 Java XML Pack 2001年冬季版 ”的新闻吗?去sun把它下载下来,然后 在你的java classpath中包含xalan.jar和xerces.jar两个jar文件。
然后试一下下面的程序
import java.io.*;
import java.util.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException ;
import org.xml.sax.SAXParseException;
import java.io.IOException;
import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
//..........
//...........
Document document;
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
//factory.setValidating(true);
//factory.setNamespaceAware(true);
try {
String realpathname=request.getRealPath("/xml/menu.xml");
File f = new File(realpathname);
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(f);
// Use a Transformer for output
TransformerFactory tFactory =
TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
}
catch (TransformerConfigurationException tce) {
// Error generated by the parser
System.out.println ("n** Transformer Factory error");
System.out.println(" " + tce.getMessage() );
// Use the contained exception, if any
Throwable x = tce;
if (tce.getException() != null)
x = tce.getException();
x.printStackTrace();
} catch (TransformerException te) {
// Error generated by the parser
System.out.println ("n** Transformation error");
System.out.println(" " + te.getMessage() );
// Use the contained exception, if any
Throwable x = te;
if (te.getException() != null)
x = te.getException();
x.printStackTrace();
}
catch (SAXException sxe) {
// Error generated during parsing)
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace();
} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
pce.printStackTrace();
} catch (IOException ioe)
{
// I/O error
ioe.printStackTrace();
}
此程序将 xml文件读出来,然后 system.out显示出来。