<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<ServiceName SOAP-ENV:mustUnderstand="1" xmlns="urn:go.jp:xmlns:AAA.BBBB.Service">SERVICE </ServiceName>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<Data xmlns="urn:go.jp:xmlns:AAA.BBBB.Service">
<Tbl name="SYSIN">
<Rec>
<Item name="Date">20080527 </Item>
<Item name="PersonalNo">01234567 </Item>
</Rec>
</Tbl>
</Data>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>*********************************/**
* PraseCSDNXML.java
*/
package black.test; import java.io.File;
import java.io.IOException; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; /**
* @author black
* @version 1.0
* @CreateDate 2008-7-22下午05:35:56
*/
public class PraseCSDNXML { /**
* @param args
*/ File file = new File("csdnTest.xml");
public static void main(String[] args) {
new PraseCSDNXML().praseXML();
} public void praseXML() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try {
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(file); NodeList elementsList = doc.getElementsByTagName("SOAP-ENV:Envelope"); for(int i=0;i <elementsList.getLength();i++) {
Element ele = (Element) elementsList.item(i); System.out.print(ele.getNodeName() + " ");
Attr attr = ele.getAttributeNode("xmlns:SOAP-ENV");
System.out.println(attr.getName() + " = " + attr.getValue()); NodeList headerList = ele.getElementsByTagName("SOAP-ENV:Header"); for( int j=0; j <headerList.getLength(); j++ ) {
Element headEle = (Element) headerList.item(j); System.out.println(" " + headEle.getNodeName()); // <SOAP-ENV:Header> 节点 NodeList serverList = headEle.getElementsByTagName("ServiceName");
for( int k=0; k <serverList.getLength(); k++ ) { Element serEle = (Element) serverList.item(k);
Attr serAttr1 = serEle.getAttributeNode("SOAP-ENV:mustUnderstand");
Attr serAttr2 = serEle.getAttributeNode("xmlns"); // bug // System.out.println(" " + serEle.getNodeName() + ", " + serEle.getNodeValue());
System.out.print(" " + serEle.getNodeName() + " "); System.out.print(serAttr1.getName() + " = " + serAttr1.getValue() + " ");
System.out.println(serAttr2.getName() + " = " + serAttr2.getValue());
} // <SOAP-ENV:Body> 节点
NodeList bodyList = ele.getElementsByTagName("SOAP-ENV:Body"); for( int n=0; n <bodyList.getLength(); n++) {
Element bodyEle = (Element) bodyList.item(n);
NodeList dataEle = bodyEle.getElementsByTagName("Data"); System.out.println(" " + bodyEle.getNodeName()); for(int l=0; l <dataEle.getLength(); l++ ) {
Element tblEle = (Element) dataEle.item(l);
Attr attr2 = tblEle.getAttributeNode("xmlns");
System.out.print(" " + tblEle.getNodeName() + " ");
System.out.println(attr2.getName() + " = " + attr2.getValue()); NodeList tblList = tblEle.getElementsByTagName("Tbl"); for( int o=0; o <tblList.getLength(); o++ ) {
Element tEle = (Element) tblList.item(o); System.out.print(" " + tEle.getNodeName() + " "); Attr attr3 = tEle.getAttributeNode("name");
System.out.println(attr3.getName() + " = " + attr3.getValue()); NodeList recList = tEle.getElementsByTagName("Rec"); for( int p=0; p <recList.getLength(); p++) {
Element rEle = (Element) recList.item(p); System.out.println(" " + rEle.getNodeName()); NodeList itemList = rEle.getElementsByTagName("Item"); for( int u=0; u <itemList.getLength(); u++) {
Element itemEle = (Element) itemList.item(u); Attr attr4 = itemEle.getAttributeNode("name"); System.out.print(" " + itemEle.getNodeName() + " "); System.out.println(attr4.getName() + " = " + attr4.getValue()); // System.out.println(attr4.getValue());
}
}
}
}
}
}
} } catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} } }
// output SOAP-ENV:Envelope xmlns:SOAP-ENV = http://schemas.xmlsoap.org/soap/envelope/
SOAP-ENV:Header
ServiceName SOAP-ENV:mustUnderstand = 1 xmlns = urn:go.jp:xmlns:AAA.BBBB.Service
SOAP-ENV:Body
Data xmlns = urn:go.jp:xmlns:AAA.BBBB.Service
Tbl name = SYSIN
Rec
Item name = Date
Item name = PersonalNo 注: 还有个问题,就是SERVICE 20080527 01234567 的值 没有打印出来了,,,我看了半天 ,不晓得那有问题。我记得我以前的写的没什么问题。 大家帮我看看。怎么输入 元素里值。。
// csdnTest.xml 是 你要解析的XML文件。 就LZ帖出来的。
楼主【redcellx】截止到2008-07-23 13:44:37的历史汇总数据(不包括此帖):
发帖的总数量:3 发帖的总分数:80 每贴平均分数:26
回帖的总数量:3 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:3 结贴的总分数:80
无满意结贴数:0 无满意结贴分:0
未结的帖子数:0 未结的总分数:0
结贴的百分比:100.00% 结分的百分比:100.00%
无满意结贴率:0.00 % 无满意结分率:0.00 %
敬礼!
也反映了这些代码中没有打印相应TextNode值的最后循环中加System.out.println(itemEle.getTextContent());包括之前ServiceName也类似