<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetCodeResponse xmlns="http://tempuri.org/">
<GetCodeResult>你好</GetCodeResult>
</GetCodeResponse>
</soap:Body>
</soap:Envelope>请问我怎么能把<GetCodeResult>你好</GetCodeResult>里面的内容取出来
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetCodeResponse xmlns="http://tempuri.org/">
<GetCodeResult>你好</GetCodeResult>
</GetCodeResponse>
</soap:Body>
</soap:Envelope>请问我怎么能把<GetCodeResult>你好</GetCodeResult>里面的内容取出来
http://www.java2000.net/viewthread.jsp?tid=122
http://blog.csdn.net/success_dream/archive/2006/12/01/1424579.aspx
public class getXML{
public String getXmlData(String fileName ,String nodeName,String key) throws Exception{
DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
try {
String nodeValue = ""; DocumentBuilder dombuilder=domfac.newDocumentBuilder(); InputStream is=new FileInputStream(fileName); Document doc=dombuilder.parse(is); Element root=doc.getDocumentElement(); NodeList nodes=root.getChildNodes();
if(nodes!=null){
for(int i=0;i<nodes.getLength();i++){
Node node=nodes.item(i); if(node.getNodeType()==Node.ELEMENT_NODE && node.getNodeName().equals(nodeName)){
NodeList nodeChilds = node.getChildNodes();
for (int k=0;k<nodeChilds.getLength();k++){
Node nodeChild = nodeChilds.item(k);
if(nodeChild.getNodeType()==Node.ELEMENT_NODE && nodeChild.getNodeName().equals(key)){
nodeValue=nodeChild.getFirstChild().getNodeValue(); return nodeValue;
}
}
}
}
}
if (nodeValue.equals("")){
Exception e = new Exception();
throw new Exception(ConstantMessage.getMessage(ConstantMessage.E0000020),e);
}
} catch (ParserConfigurationException e) {
throw new Exception(e.getMessage(),e);
} catch (FileNotFoundException e) {
throw new Exception(e.getMessage(),e);
} catch (SAXException e) {
throw new Exception(e.getMessage(),e);
} catch (IOException e) {
throw new Exception(e.getMessage(),e);
}
return "";
}
}
实例化getXML类。
getXML test = new getXML();调用getXmlData方法,传进去三个参数。
test.getXmlData(Xml的文件名,"GetCodeResponse","GetCodeResult");看看行不行?
SAXReader reader = new SAXReader();
Document doc = reader.read(new FileReader(xml文件));
Node node = doc.selectSingleNode("/soap:Envelope/GetCodeResult");
node.getText();就是你想要的.
import java.io.IOException;
import java.util.List;import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.Text;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;public class Test {
public static void main(String[] args) throws JDOMException, IOException {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File("output.xml"));
XPath path = XPath.newInstance("//tmp:GetCodeResult/text()");
path.addNamespace("tmp", "http://tempuri.org/");
List list = path.selectNodes(doc);
for(int i = 0, k = list.size(); i < k; i++) {
String value = ((Text)list.get(i)).getText();
System.out.println(value);
}
}
}
js解析xml还是很方便的。你不防也试一试。我一直都是这么用的。
import java.io.IOException;
import java.io.StringReader;
import java.util.List;import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.Text;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;public class Test {
public static void main(String[] args) throws JDOMException, IOException {
SAXBuilder builder = new SAXBuilder();
String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
"<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"\n" +
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
"<soap:Body>\n" +
"<GetCodeResponse xmlns=\"http://tempuri.org/\">\n" +
"<GetCodeResult>你好</GetCodeResult>\n" +
"</GetCodeResponse>\n" +
"</soap:Body>\n" +
"</soap:Envelope>";
Document doc = builder.build(new StringReader(str));
XPath path = XPath.newInstance("//tmp:GetCodeResult/text()");
path.addNamespace("tmp", "http://tempuri.org/");
List list = path.selectNodes(doc);
for(int i = 0, k = list.size(); i < k; i++) {
String value = ((Text)list.get(i)).getText();
System.out.println(value);
}
}
}方法二:采用正则表达式,直接进行匹配
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Test {
public static void main(String[] args) {
String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
"<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"\n" +
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
"<soap:Body>\n" +
"<GetCodeResponse xmlns=\"http://tempuri.org/\">\n" +
"<GetCodeResult>你好</GetCodeResult>\n" +
"</GetCodeResponse>\n" +
"</soap:Body>\n" +
"</soap:Envelope>";
Pattern pattern = Pattern.compile("<GetCodeResult>(.*)</GetCodeResult>");
Matcher matcher = pattern.matcher(str);
while(matcher.find()) {
System.out.println(matcher.group(1));
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<RECSMSResponse xmlns="http://tempuri.org/">
<RECSMSResult>
<MOBody>
<content>你好</content>
</MOBody>
<MOBody>
<content>你好</content>
</MOBody>
</RECSMSResult>
</RECSMSResponse>
</soap:Body>
</soap:Envelope>
结果是这样的:
你好</content></MOBody><MOBody><content>你好
<MOBody><total_num>-1</total_num>
<this_num>-1</this_num>
<recvtel>-1</recvtel>
<sender>-1</sender>
<content>你好</content>
<recdate>-1</recdate></MOBody>
<MOBody><total_num>-1</total_num>
<this_num>-1</this_num>
<recvtel>-1</recvtel>
<sender>-1</sender>
<content>你好</content>
<recdate>-1</recdate></MOBody>
要是这样能不能先把一组的MOBody里的内容取出来,再取下一组的呢
这样可以取出每一组MOBody中所有的东西。你的文件格式怎么那么复杂?一下子一个样!没有一个结构是一样的。