写一个方法用,WebService测试
对XML解析 传参数XmlDocument xml
以前没有接触过具体的XML解析
想对这些了解下
我给你提供一个解析xml的实例,看你可不可以把他套到你的方法里了,实际上解析xml核心只有7步:
1.得到DOM解析器的工厂实例
2.从DOM工厂获得DOM解析器
3.把要解析的XML文档转化为输入流,以便DOM解析器解析它(也可以用File来处理)
4.解析XML文档的输入流,得到一个Document
5.得到XML文档的根节点
6.得到节点的子节点(5,6两步也可以像我例子中那样直接得到需要遍历的子节点,根据实际情况灵活应变)
7.循环遍历子节点,获得其属性值
到此解析完毕,xml文档解析博大精深,呵呵,应对方法也有很多,我提供的是最基础的dom解析,希望有用!
以上提供了解析的xml原文件,和负责解析的java源码.xml文档有些冗余,我删掉了一部分,没办法,希望能对你有帮助!
对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文档有些冗余,我删掉了一部分,没办法,希望能对你有帮助!
本文提供了一个使用Java解析XML文档的实例,包括获取DOM解析器、解析输入流、遍历节点并获取属性值等基本步骤。通过实例代码,详细介绍了XML解析过程,并附上XML文档供实践参考。
1万+

被折叠的 条评论
为什么被折叠?



