解析XML(WebService)

写一个方法用,WebService测试
对XML解析 传参数XmlDocument xml
以前没有接触过具体的XML解析
想对这些了解下
javacode: 
public class WebServiceDemo {
public WebServiceDemo() {
}
public XmlDocument notifyGather(XmlDocument xml)
{
return null;
}
}


我给你提供一个解析xml的实例,看你可不可以把他套到你的方法里了,实际上解析xml核心只有7步:
1.得到DOM解析器的工厂实例
2.从DOM工厂获得DOM解析器
3.把要解析的XML文档转化为输入流,以便DOM解析器解析它(也可以用File来处理)
4.解析XML文档的输入流,得到一个Document
5.得到XML文档的根节点
6.得到节点的子节点(5,6两步也可以像我例子中那样直接得到需要遍历的子节点,根据实际情况灵活应变)
7.循环遍历子节点,获得其属性值
到此解析完毕,xml文档解析博大精深,呵呵,应对方法也有很多,我提供的是最基础的dom解析,希望有用!

package test1;

import java.io.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.sun.org.apache.xpath.internal.XPathAPI;

public class csdntest2 {
public csdntest2(){
DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
@SuppressWarnings("unused")
String Person_id = "";
@SuppressWarnings({ "unused", "unused" })
String Team_id = "";
@SuppressWarnings("unused")
String Player_status = "";
@SuppressWarnings("unused")
String First_name = "";
@SuppressWarnings("unused")
String Last_name = "";
@SuppressWarnings("unused")
String Jersey_number = "";
@SuppressWarnings("unused")
String Birth_date = "";
@SuppressWarnings("unused")
String Height = "";
@SuppressWarnings("unused")
String Weight = "";
@SuppressWarnings("unused")
String Position = "";
@SuppressWarnings("unused")
String School = "";
@SuppressWarnings("unused")
String DraftYear = "";
@SuppressWarnings("unused")
String PlayerCode = "";

PrintWriter pw = null;
try {
File file = new File("d:/nba_rosters.txt");
if(!file.exists())
file.createNewFile();
pw = new PrintWriter(new FileOutputStream("d:/nba_rosters.txt",true));

DocumentBuilder dombuilder=domfac.newDocumentBuilder();
InputStream is=new FileInputStream("d:/nba_rosters.xml");
Document doc = dombuilder.parse(is);
NodeList Player_info = XPathAPI.selectNodeList(doc.getDocumentElement(), "/Msg_file/Game/Msg_Roster/Player_info");
if(Player_info!=null){
System.out.println("("+Player_info.getLength()+")");
for(int i = 0;i < Player_info.getLength();i ++){
Node Player = Player_info.item(i);
Person_id = Player.getAttributes().getNamedItem("Person_id").getNodeValue();
Team_id = Player.getAttributes().getNamedItem("Team_id").getNodeValue();
Player_status = Player.getAttributes().getNamedItem("Player_status").getNodeValue();
First_name = Player.getAttributes().getNamedItem("First_name").getNodeValue();
Last_name = Player.getAttributes().getNamedItem("Last_name").getNodeValue();
Jersey_number = Player.getAttributes().getNamedItem("Jersey_number").getNodeValue();
Birth_date = Player.getAttributes().getNamedItem("Birth_date").getNodeValue();
Height = Player.getAttributes().getNamedItem("Height").getNodeValue();
Weight = Player.getAttributes().getNamedItem("Weight").getNodeValue();
Position = Player.getAttributes().getNamedItem("Position").getNodeValue();
School = Player.getAttributes().getNamedItem("School").getNodeValue();
DraftYear = Player.getAttributes().getNamedItem("DraftYear").getNodeValue();
PlayerCode = Player.getAttributes().getNamedItem("PlayerCode").getNodeValue();

pw.println(Person_id);
}
pw.close();
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}

finally{
if(pw != null)
pw.close();
}

}
public static void main(String[] args) {
new csdntest2();
}
}


以上提供了解析的xml原文件,和负责解析的java源码.xml文档有些冗余,我删掉了一部分,没办法,希望能对你有帮助!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值