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文件实例的更多相关文章

  1. 使用DOM解析xml文件

    使用DOM解析xml文件 要解析的xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <Langu ...

  2. DOM解析XML文件例子

    DOM解析XML文件是一次性将目标文件中的所有节点都读入,然后再进行后续操作的方式. 一般分为以下几步: 1. 定义好目标XML文件路径path . 2. 实例化DOM解析工厂对象 ,Document ...

  3. JAVA中使用DOM解析XML文件

    XML是一种方便快捷高效的数据保存传输的格式,在JSON广泛使用之前,XML是服务器和客户端之间数据传输的主要方式.因此,需要使用各种方式,解析服务器传送过来的信息,以供使用者查看. JAVA作为一种 ...

  4. 使用DOM解析XML文件,、读取xml文件、保存xml、增加节点、修改节点属性、删除节点

    使用的xml文件 <?xml version="1.0" encoding="GB2312" ?> <PhoneInfo> <Br ...

  5. SAX方式解析XML文件实例

    books.XML文件: 书籍book.java实体类: public class Book { private String id; private String name; private Str ...

  6. DOM方式解析XML文件实例

    books.XML文件: <?xml version="1.0" encoding="utf-8"?><bookstore>    &l ...

  7. Qt中使用DOM解析XML文件或者字符串二(实例)

    介绍 在Qt中提供了QtXml模块实现了对XML数据的处理,我们在Qt帮助中输入关键字QtXml Module,可以看到该模块的类表.在这里我们可以看到所有相关的类,它们主要是服务于两种操作XML文档 ...

  8. Qt中使用DOM解析XML文件或者字符串(实例)

    因为需要读取配置文件,我的配置文件采用xml:因此编写了使用qt读取xml文件内容的代码,xml文件如下: <?xml version="1.0" encoding=&quo ...

  9. Dom解析XML文件具体用法

    public class Dom4j { public static void main(String[] args) throws Exception { List<Student> l ...

随机推荐

  1. Eequal sum sets

    Let us consider sets of positive integers less than or equal to n. Note that all elements of a set a ...

  2. OSGi 学习之路(4) - osgi的模块化 java在模块化的局限性

    底层代码可见性控制 Java提供了private,public,protected和package private(无修饰符)这四种访问控制级别,不过这仅仅提供了底层的OO数据封装特性.包这个概念确实 ...

  3. C#多线程实现方法——Task/Task.Factary

    原文:C#多线程实现方法--Task/Task.Factary Task 使用 Task以及Task.Factory都是在.Net 4引用的.Task跟Thread很类似,通过下面例子可以看到. st ...

  4. Mysql RR隔离更新列没有索引 会锁全表

    <pre name="code" class="html">mysql> show variables like '%tx_isolation ...

  5. cct软件测试

    <全国计算机等级考试三级教程:软件测试技术(2016年版)>根据教育部考试中心制订的<全国计算机等级考试三级软件测试技术考试大纲(2013年版)>编写而成.主要内容包括软件测试 ...

  6. ibatis新手入门

    ibatis 是什么 iBATIS是以SQL为中心的持久化层框架. 能支持懒载入.关联查询.继承等特性. iBATIS不同于一般的OR映射框架. OR映射框架,将数据库表.字段等映射到类.属性,那是一 ...

  7. 1038. Recover the Smallest Number (30) - 字符串排序

    题目例如以下: Given a collection of number segments, you are supposed to recover the smallest number from ...

  8. 所有CN_消息的说明

    Notification Message Corresponding WindowsConstant Message Description cn_CharToItem wm_CharToItem T ...

  9. 创作gtk源码级vim帮助文档 tags

    创作gtk源码级vim帮助文档 tags 缘由 那只有看到源码了.在linux源码上有个网站 http://lxr.linux.no /+trees, 可以很方面的查出相应版本的代码实现,gtk没有. ...

  10. struts2 与 OGNL 表达式,jsp中 利用ognl 在valuestack中取值

    在Struts2中,一个请求在终于到达Action的方法之前,Action对象本身会被压入ValueStack(实际上就是放到ValueStack的CompoundRoot中),所以Action对象是 ...