XML文件:
response:
<?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Body xmlns:m="http://www.nwpu.edu.cn/soa/xml/test "> <m:GetWeatherResponse> <m:Temperature>13.2</m:Temperature> <m:Weather >sunny</m:Weather > </m:GetWeatherResponse> </soap:Body> </soap:Envelope>
request:
<?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Body xmlns:n="http://www.nwpu.edu.cn/soa/xml/test"> <n:GetWeather> <n:CityName>西安</n:CityName> </n:GetWeather> </soap:Body> </soap:Envelope>
解析函数:
package com.wjy.marshal; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class GetCityName { private String xmlFilePath="C://Documents and Settings/Administrator/桌面/request.xml"; public String getCityName() { String result = ""; try { // step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器) DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //System.out.println("class name: " + dbf.getClass().getName()); // step 2:获得具体的dom解析器 DocumentBuilder db = dbf.newDocumentBuilder(); //System.out.println("class name: " + db.getClass().getName()); // step3: 解析一个xml文档,获得Document对象(根结点) Document document = db.parse(new File(xmlFilePath)); NodeList nodeList=document.getElementsByTagName("n:GetWeather"); Element element=(Element)nodeList.item(0); result=element.getElementsByTagName("n:CityName").item(0).getFirstChild().getNodeValue(); } catch (Exception e) { // TODO: handle exception } return result; } }
package com.wjy.marshal; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class GetCityWeather { private String xmlFilePath="C://Documents and Settings/Administrator/桌面/response.xml"; public String getCityWeather() { String tempurature = ""; String weather=""; try { // step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器) DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //System.out.println("class name: " + dbf.getClass().getName()); // step 2:获得具体的dom解析器 DocumentBuilder db = dbf.newDocumentBuilder(); //System.out.println("class name: " + db.getClass().getName()); // step3: 解析一个xml文档,获得Document对象(根结点) Document document = db.parse(new File(xmlFilePath)); NodeList nodeList=document.getElementsByTagName("m:GetWeatherResponse"); Element element=(Element)nodeList.item(0); tempurature=element.getElementsByTagName("m:Temperature").item(0).getFirstChild().getNodeValue(); weather=element.getElementsByTagName("m:Weather").item(0).getFirstChild().getNodeValue(); System.out.println(tempurature+" "+weather); } catch (Exception e) { // TODO: handle exception } return tempurature; } }
主函数:
import com.wjy.marshal.GetCityName; import com.wjy.marshal.GetCityWeather; public class zhu { public static void main(String args[]){ GetCityWeather getCityWeather=new GetCityWeather(); getCityWeather.getCityWeather(); GetCityName getCityName=new GetCityName(); System.out.println(getCityName.getCityName()); } }
相关推荐
android为我们提供了多种解析方式,DOM解析,代码操作简单,一直为解析xml文件的一个不错的选择
java dom 解析 xml 实例 介绍w3c中的元素的用法,以及在解析xml过程中用到的API进行详细解释
DOM解析XML应用实例(入门经典案例),直接在Myeclipse值导入即可运行
包括android代码实例以及个人对Dom解析xml文件的总结文档。
SAX解析XML文件的实例。一个项目同时用dom解析和sax解析xml文件貌似会报错,项目框架建一直是用sax和dom4j解析...当我用dom解析xml文件。导入包后就报错识别不了xml文件的编码格式。于是做了一个sax解析xml文件的实例
很实用的一个dom解析xml实例,很容易上手
JAVA 解析XML和生成XML文档源码。比较全 1.DOM生成和解析XML文档 2.SAX生成和解析XML文档 3.DOM4J生成和解析XML文档 4.JDOM生成和解析XML
dom4j解析xml文件代码示例 dom4j解析xml文件代码示例 dom4j解析xml文件代码示例
dom4j解析xml实例,dom4j解析xml实例,dom4j解析xml实例,
android上使用DOM解析XML的简单示例。android4.0以上。
asp使用xmldom对象解析xml文件示例。
在页面中使用dom节点解析xml文件实例
主要介绍了JAVA DOM解析XML文件过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
dom4j是一种解析XML文档的开放源代码XML框架。本文介绍如何使用包含在dom4j中的解析器创建并修改XML文档。dom4j API包含一个解析XML文档的工具。本文中将使用这个解析器创建一个示例XML文档,然后使用同一个解析器...
JSP基于dom解析xml实例详解.docx
dom,dom4j,sax解析xml 文件经典实例,内置详细注解
android dom解析XMl文件,这是一个简单的小示例,希望对大家有所帮助
简单的sax和dom解析xml实例 注意 在android上运行的.
Java解析XML的四种方法 1. DOM生成和解析XML文档 2. SAX生成和解析XML文档 3. DOM4J生成和解析XML文档 4. JDOM生成和解析XML