开发手册 欢迎您!
软件开发者资料库

Java DocumentBuilderFactory(javax.xml)通过XPath解析xml文件

Java中解析xml文件方法有3种:DOM 解析 XML、SAX 解析 XML、Digester 解析 XML,本文主要介绍Dom解析,DocumentBuilderFactory(javax.xml)通过XPath方式解析xml文件。

1、配置Maven依赖


    javax.xml
    jaxp-api
    1.4

注意:不使用maven项目,可以手动引用Jar包。

2、解析xml文件代码

        http://www.rgagnonjavahowto.htm  taxi           http://www.rgagnon/pbhowto.htm       http://www.rgagnon/pbhowtonew.htm            http://www.rgagnon/jshowto.htm          http://www.rgagnon/vbshowto.htm  

1)获取第url中的地址

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document doc = builder.parse();XPathFactory xPathfactory = XPathFactory.newInstance();XPath xpath = xPathfactory.newXPath();//获取第一个url的地址XPathExpression expr = xpath.compile("/howto/topic[@name='PowerBuilder']/url/text()");//获取第二个url的地址//XPathExpression expr = xpath.compile("/howto/topic[@name='PowerBuilder']/url[2]/text()");String url = expr.evaluate(doc, XPathConstants.STRING);//如果不确定最终获取元素的个数//XPathExpression expr = xpath.compile("/howto/topic[@name='PowerBuilder']/url");//NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

2)项目代码

import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class DOMParser {   DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();   //Load and parse XML file into DOM   public Document parse(String filePath) {      Document document = null;      try {         //DOM parser instance         DocumentBuilder builder = builderFactory.newDocumentBuilder();         //parse an XML file into a DOM tree         document = builder.parse(new File(filePath));      } catch (ParserConfigurationException e) {         e.printStackTrace();       } catch (SAXException e) {         e.printStackTrace();      } catch (IOException e) {         e.printStackTrace();      }      return document;   }   public static void main(String[] args) {         DOMParser parser = new DOMParser();         Document document = parser.parse("demo.xml");         Document doc = builder.parse();        XPathFactory xPathfactory = XPathFactory.newInstance();XPath xpath = xPathfactory.newXPath();//获取第一个url的地址XPathExpression expr = xpath.compile("/howto/topic[@name='PowerBuilder']/url/text()");//获取第二个url的地址//XPathExpression expr = xpath.compile("/howto/topic[@name='PowerBuilder']/url[2]/text()");String url = expr.evaluate(doc, XPathConstants.STRING);//如果不确定最终获取元素的个数//XPathExpression expr = xpath.compile("/howto/topic[@name='PowerBuilder']/url");//NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);  } }