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. 开源解析器--ANTLR

      序言 有的时候,我还真是怀疑过上本科时候学的那些原理课究竟是不是在浪费时间.比方学完操作系统原理之后我们并不能自己动手实现一个操作系统:学完数据库原理我们也不能弄出个像样的DBMS出来:相同,学完 ...

  2. 进阶:案例五: Dynamic 创建 Business Graphic

    效果图: step: 无需节点无需UI 1.添加属性 2.代码: method WDDOMODIFYVIEW . DATA:lr_graph TYPE REF TO cl_wd_business_gr ...

  3. Selenium Webdriver ie 浏览器

    webDriver 在测试ie 的时候会遇到很多的问题,记录下: 1.需要ie的driver驱动 需要下载 IEDriverServer.exe 并把这个驱动放在系统ie 的文件夹下 C:\Progr ...

  4. haproxy redirect location和redirect prefix

    <pre name="code" class="html">redirect location <loc> [code <code ...

  5. mysql基础:mysql列类型--时间和日期

    mysql列类型--整型 http://blog.csdn.net/jk110333/article/details/9342283 mysql列类型--字符串http://blog.csdn.net ...

  6. Microsoft JET Database Engine 错误 '80004005'不是一个有效的路径。 确定路径名称拼写是否正确,以及是否连接到文件存放的服务器。

  7. 与众不同 windows phone (19) - Device(设备)之陀螺仪传感器, Motion API

    原文:与众不同 windows phone (19) - Device(设备)之陀螺仪传感器, Motion API [索引页][源码下载] 与众不同 windows phone (19) - Dev ...

  8. 14.4.9 Configuring Spin Lock Polling 配置Spin lock 轮询:

    14.4.9 Configuring Spin Lock Polling 配置Spin lock 轮询: 很多InnoDB mutexes 和rw-locks 是保留一小段时间,在一个多核系统, 它可 ...

  9. C语言信号学习笔记

    在C语言中,对于错误有很多处理方式.然而,今天学习了信号处理,感觉这种处理方式十分灵活,特此记录. 关于信号处理的函数包含于头文件<signal.h>中.所谓的信号,多指出乎程序员意料的行 ...

  10. Apache Commons IO入门教程(转)

    Apache Commons IO是Apache基金会创建并维护的Java函数库.它提供了许多类使得开发者的常见任务变得简单,同时减少重复(boiler-plate)代码,这些代码可能遍布于每个独立的 ...