java解析、读取xml文件,org.w3c.dom.Document

2 篇文章 0 订阅

Java DOM 的 API:
1.解析器工厂类:DocumentBuilderFactory

创建的方法:DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

2.解析器:DocumentBuilder

创建方法:通过解析器工厂类来获得 DocumentBuilder db = dbf.newDocumentBuilder();

3.文档树模型Document

创建方法:a.通过xml文档 Document doc = db.parse("bean.xml");  

     b.将需要解析的xml文档转化为输入流 InputStream is = new FileInputStream("bean.xml"); Document doc = db.parse(is); 

Document对象代表了一个XML文档的模型树,所有的其他Node都以一定的顺序包含在Document对象之内,排列成一个树状结构,以后对XML文档的所有操作都与解析器无关,

直接在这个Document对象上进行操作即可;

package util;

import java.io.File;
import java.io.FileInputStream;
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.NodeList;
import org.xml.sax.SAXException;

public class XmlUtil {
	
	public static void main(String[] args){
		try {
			//得到DOM解析器的工厂实例
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			//从DOM工厂中获得DOM解析器
			DocumentBuilder db = dbf.newDocumentBuilder();
			
			/*String xmlFile = "web-context.xml";
			File file = new File(xmlFile);
			Document doc = db.parse(file);*/
			
			/*String xmlFile = "web-contextt.xml";
			FileInputStream is = new FileInputStream(xmlFile);
			Document doc = db.parse(is);*/
			
			//把要解析的xml文档读入DOM解析器
			String xmlFile = "web-context.xml";
			Document doc = db.parse(xmlFile);
			
			Element root = doc.getDocumentElement();
			System.out.println(root.getTagName());

			NodeList nodes =  doc.getElementsByTagName("bean");
			
			int length = nodes.getLength();
			for(int i=0;i<length;i++){
				Element el = (Element)nodes.item(i);
				String name = el.getAttribute("name");
				name = name.replace("/", "");
				System.out.println(name);
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

<?xml version="1.0" encoding="GB2312" ?>
<beans>
	<description>定义web层的资源</description>
   	<bean name="/unitPrice.do">
    	<property name="commandName">
      		<value>form</value>
  		</property>
  	</bean>
  
   	<bean name="/assetCounterAsset.do">
	    <property name="commandName">
	      <value>form</value>
	  	</property>
  	</bean>

  	<bean name="/projectByNode.do">
		<description>下拉选择框的公共Controller</description>
		<property name="viewName">
			<value>pubView</value>
		</property>
  	</bean>

 	<bean name="/assetParamValue.do">
    	<property name="commandName">
      		<value>form</value>
  		</property>
  	</bean>
</beans>






  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java读取Maven的pom.xml文件可以使用以下步骤: 1. 创建文件对象 首先需要创建一个File对象,用于表示pom.xml文件的路径。可以使用以下代码创建File对象: ``` File pomFile = new File("pom.xml"); ``` 2. 加载pom.xml文件 使用JavaDOM解析器来加载pom.xml文件。可以使用以下代码来加载pom.xml文件: ``` DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(pomFile); ``` 3. 获取根元素 通过Document对象获取pom.xml文件的根元素。可以使用以下代码获取根元素: ``` Element root = doc.getDocumentElement(); ``` 4. 获取元素值 通过Element对象获取pom.xml文件中的元素值。可以使用以下代码获取元素值: ``` String groupId = root.getElementsByTagName("groupId").item(0).getTextContent(); String artifactId = root.getElementsByTagName("artifactId").item(0).getTextContent(); String version = root.getElementsByTagName("version").item(0).getTextContent(); ``` 以上代码将获取pom.xml文件中的groupId、artifactId和version元素的值。您可以根据需要获取其他元素的值。 完整的代码示例: ``` import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; public class ReadPomXml { public static void main(String[] args) throws Exception { File pomFile = new File("pom.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(pomFile); Element root = doc.getDocumentElement(); String groupId = root.getElementsByTagName("groupId").item(0).getTextContent(); String artifactId = root.getElementsByTagName("artifactId").item(0).getTextContent(); String version = root.getElementsByTagName("version").item(0).getTextContent(); System.out.println("groupId: " + groupId); System.out.println("artifactId: " + artifactId); System.out.println("version: " + version); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值