首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

java解析XML,该怎么解决

2012-10-23 
java解析XMLXML code?xml version1.0 encodingGBK?respcode1000/codebalance0.855/balan

java解析XML

XML code
<?xml version="1.0" encoding="GBK"?><resp>    <code>1000</code>    <balance>0.855</balance>    <pkg count="0"/>    <smsPrice>0.045</smsPrice>    <voicePrice>0.03</voicePrice>    <signZH>大众</signZH><signEN>dz</signEN></resp>


我要得到resp下面所有节点下面的数据。

[解决办法]
用dom4j:
SAXReader saxReader = new SAXReader();
StringReader stringReader = new StringReader(xml);
Document document = null;
try {
document = saxReader.read(stringReader);
} catch (DocumentException e) {
e.printStackTrace();
logger.error("创建Document对象错误", e);
return null;
}

然后 document.getRootElement().elements ()
[解决办法]
package com.testfile;
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.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class Track {

public static int code;
public static double balance;
public static String pkgCount;
public static double smsPrice;
public static double voicePrice;
public static String signZH;
public static String signEN;
public static String keyspace;

public static void main(String[] X) {

readPropertyFile("C:\\EclipseSet3.6_V01\\workspace\\NewFile.xml");

System.out.println("code = " + code);
System.out.println("balance = " + balance);
System.out.println("pkgCount = " + pkgCount);
System.out.println("smsPrice = " + smsPrice);
System.out.println("voicePrice = " + voicePrice);
System.out.println("signZH = " + signZH);
System.out.println("signEN = " + signEN);
System.out.println("keyspace = " + keyspace);

}

private static void readPropertyFile(String filename) {

File file;
DocumentBuilderFactory factory;
DocumentBuilder builder;
Document doc;

try {

file = new File(filename);
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
doc = builder.parse(file);

} catch(ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException();
} catch (SAXException e) {
throw new RuntimeException();
}

// path開始
NodeList childs = doc.getChildNodes();

for(int i = 0; i < childs.getLength(); i++) {
Node n = childs.item(i);
Element e = (Element)n;

if(e.getNodeType() != Node.ELEMENT_NODE)
continue;

// resp
if(!"resp".equals(e.getTagName()))
continue;

// resp的子要素
NodeList paramNode = n.getChildNodes();
for(int j = 0; j < paramNode.getLength(); j++) {
Node n2 = (Node)paramNode.item(j);

if(n2.getNodeType() != Node.ELEMENT_NODE) continue;



// code
if("code".equals(n2.getNodeName())) {

code = Integer.parseInt(n2.getFirstChild().getNodeValue());
// balance
} else if("balance".equals(n2.getNodeName())) {

balance = Double.parseDouble(n2.getFirstChild().getNodeValue());

// pkg
} else if("pkg".equals(n2.getNodeName())) {
pkgCount = "0";
} else if ("smsPrice".equals(n2.getNodeName())) {
smsPrice = Double.parseDouble(n2.getFirstChild().getNodeValue());
} else if ("voicePrice".equals(n2.getNodeName())) {
voicePrice = Double.parseDouble(n2.getFirstChild().getNodeValue());
} else if ("signZH".equals(n2.getNodeName())) {
signZH = n2.getFirstChild().getNodeValue();
} else if ("signEN".equals(n2.getNodeName())) {
signEN = n2.getFirstChild().getNodeValue();
}
}
}
}
}


说明,"C:\\EclipseSet3.6_V01\\workspace\\NewFile.xml" 为你的xml存放的路径
运行结果: 
code = 1000
balance = 0.855
pkgCount = 0
smsPrice = 0.045
voicePrice = 0.03
signZH = 大众
signEN = dz
[解决办法]
哦 上面的少删除一句话
 System.out.println("keyspace = " + keyspace);

热点排行