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

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

1、配置Maven依赖

<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxp-api</artifactId>
<version>1.4</version>
</dependency>

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

2、解析xml文件代码

<?xml version="1.0"?>
<howto>
  <topic name="Java">
      <url>http://www.rgagnonjavahowto.htm</url>
  <car>taxi</car>
  </topic>
  <topic name="PowerBuilder">
       <url>http://www.rgagnon/pbhowto.htm</url>
       <url>http://www.rgagnon/pbhowtonew.htm</url>
  </topic>
  <topic name="Javascript">
        <url>http://www.rgagnon/jshowto.htm</url>
  </topic>
 <topic name="VBScript">
       <url>http://www.rgagnon/vbshowto.htm</url>
 </topic>
 </howto>

1)获取第url中的地址

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(<uri_as_string>);
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 >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(<uri_as_string>);
        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);
  } 
}
特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。