java中解析xml

public class Test {
public static void main(String[] arc) throws RuntimeException {

try {
SAXReader reader = new SAXReader();
//以下是要输入的xml
org.dom4j.Document doc = reader.read("C:\\11.xml");
String aa = doc.asXML();
EntrustUpLoadCommand pd = new EntrustUpLoadCommand();
pd.setXML(aa);
pd = (EntrustUpLoadCommand) pd.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
}

--------------------------

//用于把xml文件中属性值转为map 其中 name为map的key,value为map中的value
public static Map getProperties(String xml) throws CommandException {
Map prop = new HashMap();
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new StringReader(xml));
Element root = doc.getRootElement();
Element pro = root.getChild("properties");
if (null != pro) {
List proList = pro.getChildren("cell");


for (int i = 0; i < proList.size(); i++) {
Element cell = (Element) proList.get(i);
prop.put(cell.getAttributeValue("name").toLowerCase(), cell.getAttributeValue("value"));
}
}

getvoList(root, prop);
} catch (JDOMException e) {
throw new CommandException("XML报文有问题");
}

return prop;
}

//用于把Element文件中属性值转为map[] 其中 name为map的key,value为map中的value
//此方法主要是单list转map
private static void getvoList(Element root, Map in) throws CommandException {

try {
List pro = root.getChildren("arrayList");
log.debug("pro.size() = " + (null == pro ? "0" : "" + pro.size()));
if (null != pro) {
for (int k = 0; k < pro.size(); k++) {
List prop = new ArrayList();
Element prof = (Element) pro.get(k);
in.put(prof.getAttributeValue("name"), prop);
List pror = prof.getChildren("vo");
for (int j = 0; j < pror.size(); j++) {
Element proc = (Element) pror.get(j);
List proList = proc.getChildren("cell");
Map propc = new HashMap();
if (null != proList) {
for (int i = 0; i < proList.size(); i++) {
Element cell = (Element) proList.get(i);
propc.put(cell.getAttributeValue("name").toLowerCase(), cell.getAttributeValue("value"));
}
prop.add(propc);
}
getvoList2(proc,in);//-----------
}

}
}
} catch (Exception e) {
throw new CommandException("XML报文解析vo有问题");
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值