java 读取xml 属性_JAVA读取XML文件并解析获取元素、属性值、子元素信息

packagecom.huishe.testOfSpring;importjava.io.FileInputStream;importjava.io.InputStream;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.parsers.DocumentBuilderFactory;importorg.w3c.dom.Document;importorg.w3c.dom.Element;importorg.w3c.dom.Node;importorg.w3c.dom.NodeList;public classXMLParse {public static void main(String[] args) throwsException {//1-获取XML-IO流

InputStream xmlInputStream = getXmlInputStream("xml/tinyioc.xml");//2-解析XML-IO流 ,获取Document 对象,以及Document对象 的根节点

Element rootElement =getRootElementFromIs(xmlInputStream);//3~5-从根元素解析得到元素

parseElementFromRoot(rootElement);

//控制台输出:

//name == HelloWorld

//className == com.huishe.HelloWord

//propertyEle: name == textone

//propertyEle: value == Hello World!

//propertyEle: name == texttwo

//propertyEle: value == Hello SUN!

}//1-获取XML-IO流

private staticInputStream getXmlInputStream(String xmlPath){

InputStream inputStream= null;try{//1-把要解析的 XML 文档转化为输入流,以便 DOM 解析器解析它

inputStream= newFileInputStream(xmlPath);

}catch(Exception e) {

e.printStackTrace();

}returninputStream;

}//2-解析XML-IO流 ,获取Document 对象,以及Document对象 的根节点

private static Element getRootElementFromIs(InputStream inputStream) throwsException {if(inputStream == null){return null;

}/** javax.xml.parsers 包中的DocumentBuilderFactory用于创建DOM模式的解析器对象 ,

* DocumentBuilderFactory是一个抽象工厂类,它不能直接实例化,但该类提供了一个newInstance方法 ,

* 这个方法会根据本地平台默认安装的解析器,自动创建一个工厂的对象并返回。*/

//2-调用 DocumentBuilderFactory.newInstance() 方法得到创建 DOM 解析器的工厂

DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();//3-调用工厂对象的 newDocumentBuilder方法得到 DOM 解析器对象。

DocumentBuilder docBuilder =factory.newDocumentBuilder();//4-调用 DOM 解析器对象的 parse() 方法解析 XML 文档,得到代表整个文档的 Document 对象,进行可以利用DOM特性对整个XML文档进行操作了。

Document doc =docBuilder.parse(inputStream);//5-得到 XML 文档的根节点

Element root =doc.getDocumentElement();//6-关闭流

if(inputStream != null){

inputStream.close();

}returnroot;

}//3-从根元素解析得到元素

private static voidparseElementFromRoot(Element root) {

NodeList nl=root.getChildNodes();for (int i = 0; i < nl.getLength(); i++) {

Node node=nl.item(i);if (node instanceofElement) {

Element ele=(Element) node;//4-从元素解析得到属性值

getDataFromElement(ele);//5-从元素解析特定子元素并解析(以property为例)

getCertainElementFromParentElement(ele);

}

}

}//4-从元素解析得到属性值

private static voidgetDataFromElement(Element ele) {

String name= ele.getAttribute("name");//根据属性名称读取属性值

System.out.println("name == " +name);

String className= ele.getAttribute("class");

System.out.println("className == " +className);

}//5-从元素解析特定子元素并解析(以property为例)

private static voidgetCertainElementFromParentElement(Element ele) {

NodeList propertyEleList= ele.getElementsByTagName("property");//根据标签名称获取标签元素列表

for (int i = 0; i < propertyEleList.getLength(); i++) {

Node node=propertyEleList.item(i);if (node instanceofElement) {

Element propertyEle=(Element) node;

String name= propertyEle.getAttribute("name");

System.out.println("propertyEle: name == " +name);

String value= propertyEle.getAttribute("value");

System.out.println("propertyEle: value == " +value);

}

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值