67,517
社区成员
发帖
与我相关
我的任务
分享
/**
*
*/
package com.ecc.test1105;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* @author Liu Shijie
*
*/
public class XmlParser {
/**
* @param args
*/
public static void main(String[] args) {
try {
//Resource r = new ClassPathResource("MyXml.xml");//使用Spring类比较方便
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
//Document d = db.parse(r.getInputStream());
Document d = db.parse(new File("MyXml.xml"));
NodeList nl = d.getChildNodes();
// System.out.println(nl.getLength());
Node n = nl.item(0);
NodeList cnl = n.getChildNodes();
// System.out.println(cnl.getLength());
for (int i = 0; i < cnl.getLength(); i++) {
Node cn = cnl.item(i);
if (cn.getNodeType() == Node.ELEMENT_NODE) {
// System.out.println(cn.getNodeName());
if (cn.getNodeName().equals("Content")) {
String cnv = cn.getFirstChild().getNodeValue();
// System.out.println(cn.getFirstChild().getNodeValue());
Document cnd = db.parse(new InputSource(
new StringReader(cnv)));
NodeList cnnl = cnd.getChildNodes();
// System.out.println(cnnl.getLength());
Node cnn = cnnl.item(0);
NodeList cncnl = cnn.getChildNodes();
// System.out.println(cncnl.getLength());
for (int j = 0; j < cncnl.getLength(); j++) {
Node cncn = cncnl.item(j);
if (cncn.getNodeType() == Node.ELEMENT_NODE) {
// System.out.println(cncn.getNodeName());
if (cncn.getNodeName().equals("AccNbr")) {
System.out.println(cncn.getFirstChild()
.getNodeValue());
}
}
}
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}