DOM解析XML文件实例
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();
result=element.getElementsByTagName("n:CityName").item().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();
tempurature=element.getElementsByTagName("m:Temperature").item().getFirstChild().getNodeValue();
weather=element.getElementsByTagName("m:Weather").item().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());
}
}
DOM解析XML文件实例的更多相关文章
- 使用DOM解析xml文件
使用DOM解析xml文件 要解析的xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <Langu ...
- DOM解析XML文件例子
DOM解析XML文件是一次性将目标文件中的所有节点都读入,然后再进行后续操作的方式. 一般分为以下几步: 1. 定义好目标XML文件路径path . 2. 实例化DOM解析工厂对象 ,Document ...
- JAVA中使用DOM解析XML文件
XML是一种方便快捷高效的数据保存传输的格式,在JSON广泛使用之前,XML是服务器和客户端之间数据传输的主要方式.因此,需要使用各种方式,解析服务器传送过来的信息,以供使用者查看. JAVA作为一种 ...
- 使用DOM解析XML文件,、读取xml文件、保存xml、增加节点、修改节点属性、删除节点
使用的xml文件 <?xml version="1.0" encoding="GB2312" ?> <PhoneInfo> <Br ...
- SAX方式解析XML文件实例
books.XML文件: 书籍book.java实体类: public class Book { private String id; private String name; private Str ...
- DOM方式解析XML文件实例
books.XML文件: <?xml version="1.0" encoding="utf-8"?><bookstore> &l ...
- Qt中使用DOM解析XML文件或者字符串二(实例)
介绍 在Qt中提供了QtXml模块实现了对XML数据的处理,我们在Qt帮助中输入关键字QtXml Module,可以看到该模块的类表.在这里我们可以看到所有相关的类,它们主要是服务于两种操作XML文档 ...
- Qt中使用DOM解析XML文件或者字符串(实例)
因为需要读取配置文件,我的配置文件采用xml:因此编写了使用qt读取xml文件内容的代码,xml文件如下: <?xml version="1.0" encoding=&quo ...
- Dom解析XML文件具体用法
public class Dom4j { public static void main(String[] args) throws Exception { List<Student> l ...
随机推荐
- Delphi XE2 升级项目编译ralease版本,无法添加UAC解决方法
我今天把一个原来是Delphi2007的工程升级到了Delphi XE2,在编译ralease版本时候,发现无法添加UAC,我觉得可能是升级的原因,随后我用XE2新建了一个工程,但是在编译raleas ...
- find: paths must precede expression(转)
find: paths must precede expressionUsage: find [-H] [-L] [-P] [path...] [expression] 然后就上网查了一下,结果搜索到 ...
- Swift - 数组排序方法(附样例)
下面通过一个样例演示如何对数组元素进行排序.数组内为自定义用户对象,最终要实现按用户名排序,数据如下: 1 2 3 4 var userList = [UserInfo]() userList.app ...
- Android使用ksoap2-android调用WebService学习
之前主要做客户端UI交互,很少处理数据和接触服务端,但现在的移动设备根本不可能离得开网络连接,数据的交换.最近学习的是在android端如何去调用远程WebService,都说WebService是一 ...
- [C++STDlib基础]关于日期时间的操作——C++标准库头文件<ctime>
总结 /* A.头文件<ctime> #if _GLOBAL_USING && !defined(RC_INVOKED) _STD_BEGIN 1.四个数据类型 using ...
- How to get the source code of the chromium of the specified revision
I'd like to get the source code of the chromium 34.0.1847.9. gclient config http://src.chromium.org/ ...
- 在Ubuntu Desktop打开终端的2种方式
共有3中方法: 1.在Ubuntu左上角选择File/Open in Terminal 2.快捷键alt+F2调出Run a Command,输入gnome-terminal 添加右键支持 在终端,输 ...
- 计算机视觉与模式识别代码合集第二版two
Topic Name Reference code Image Segmentation Segmentation by Minimum Code Length AY Yang, J. Wright, ...
- 在web网页中正确使用图片格式
今天又看了一遍淘宝平四分享的PPT,以前转载网址:http://blog.sina.com.cn/s/blog_995c1f6301017fd2.html
- YUV格式具体解释
YUV是指亮度參量和色度參量分开表示的像素格式,而这样分开的优点就是不但能够避免相互干扰,还能够减少色度的採样率而不会对图像质量影响太大.YUV是一个比較笼统地说法,针对它的详细排列方式,能够分为非常 ...