Java中解析xml文件方法有3种:DOM 解析 XML、SAX 解析 XML、Digester 解析 XML,本文主要介绍Dom解析,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 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(<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);
  } 
}

推荐文档