用java解析获得xml中的值
StringinputBizInfo="<?xmlversion=\"1.0\"encoding=\"utf-8\"?>\n"+"<EAS>\n"+"<Header>\n...
String inputBizInfo = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
"<EAS>\n" +
" <Header>\n" +
" <TransID>e688030e-f86f-455b-b143-58871084670b</TransID>\n" +
" <TransTime>2015-05-29 02:12:15</TransTime>\n" +
" <DataType>Vendor/Customer</DataType>\n" +
" <OptCatalog>Single</OptCatalog>\n" +
" <OptType>Update/Create</OptType>\n" +
" <RowCount>1</RowCount>\n" +
" </Header>\n" +
" <MasterData>\n" +
" <Field name=\"UnitClass\">\n" +
" <!--单位分类-->2000 单位分类 测试一下\n" +
" </Field>\n" +
" <Field name=\"AccountGroup\">\n" +
" <!--账户组-->\n" +
" </Field>\n" +
" <Field name=\"Name1 \">\n" +
" <!--单位全称-->1111 文具公司\n" +
" </Field>\n" +
" </MasterData>\n" +
" <ChangeDetail>\n" +
" <ChangeFields name=\"Name1\">\n" +
" <OldValue>文具</OldValue>\n" +
" <NewValue>皮包</NewValue>\n" +
" <OperationType>修改</OperationType>\n" +
" </ChangeFields>\n" +
" </ChangeDetail>\n" +
"</EAS>" ;
我想获得这个xml中遍历分别获得2000 单位分类 测试一下 ,空, 1111 文具公司 这样的3组值,每组值中的数据 用空格隔开放到数组中去,应该怎么办呢 展开
"<EAS>\n" +
" <Header>\n" +
" <TransID>e688030e-f86f-455b-b143-58871084670b</TransID>\n" +
" <TransTime>2015-05-29 02:12:15</TransTime>\n" +
" <DataType>Vendor/Customer</DataType>\n" +
" <OptCatalog>Single</OptCatalog>\n" +
" <OptType>Update/Create</OptType>\n" +
" <RowCount>1</RowCount>\n" +
" </Header>\n" +
" <MasterData>\n" +
" <Field name=\"UnitClass\">\n" +
" <!--单位分类-->2000 单位分类 测试一下\n" +
" </Field>\n" +
" <Field name=\"AccountGroup\">\n" +
" <!--账户组-->\n" +
" </Field>\n" +
" <Field name=\"Name1 \">\n" +
" <!--单位全称-->1111 文具公司\n" +
" </Field>\n" +
" </MasterData>\n" +
" <ChangeDetail>\n" +
" <ChangeFields name=\"Name1\">\n" +
" <OldValue>文具</OldValue>\n" +
" <NewValue>皮包</NewValue>\n" +
" <OperationType>修改</OperationType>\n" +
" </ChangeFields>\n" +
" </ChangeDetail>\n" +
"</EAS>" ;
我想获得这个xml中遍历分别获得2000 单位分类 测试一下 ,空, 1111 文具公司 这样的3组值,每组值中的数据 用空格隔开放到数组中去,应该怎么办呢 展开
1个回答
展开全部
package com.applet;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class B {
public static void main(String[] args) {
String inputBizInfo = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
+ "<EAS>\n"
+ " <Header>\n"
+ " <TransID>e688030e-f86f-455b-b143-58871084670b</TransID>\n"
+ " <TransTime>2015-05-29 02:12:15</TransTime>\n"
+ " <DataType>Vendor/Customer</DataType>\n"
+ " <OptCatalog>Single</OptCatalog>\n"
+ " <OptType>Update/Create</OptType>\n"
+ " <RowCount>1</RowCount>\n" + " </Header>\n"
+ " <MasterData>\n" + " <Field name=\"UnitClass\">\n"
+ " <!--单位分类-->2000 单位分类 测试一下\n" + " </Field>\n"
+ " <Field name=\"AccountGroup\">\n" + " <!--账户组-->\n"
+ " </Field>\n" + " <Field name=\"Name1 \">\n"
+ " <!--单位全称-->1111 文具公司\n" + " </Field>\n"
+ " </MasterData>\n" + " <ChangeDetail>\n"
+ " <ChangeFields name=\"Name1\">\n"
+ " <OldValue>文具</OldValue>\n"
+ " <NewValue>皮包</NewValue>\n"
+ " <OperationType>修改</OperationType>\n"
+ " </ChangeFields>\n" + " </ChangeDetail>\n" + "</EAS>";
// 我想获得这个xml中遍历分别获得
// 2000 单位分类 测试一下 ,空, 1111 文具公司 这样的3组值,每组值中的数据 用空格隔开放到数组中去,应该怎么办呢
try {
SAXReader reader = new SAXReader();
String txt = inputBizInfo.replaceAll("\n", "");
// Document doc = reader.read(new StringReader(txt));
// Document doc = reader.read(new
// File("F:\\zz\\FileRecv\\MyWebSocket\\src\\com\\applet\\NewFile.xml"));
// Document doc = reader.read(new ByteArrayInputStream(txt
// .getBytes("UTF-8")));
// System.err.println(txt);
// System.err.println(root.attribute(0).getName());
Document doc = DocumentHelper.parseText(txt);
Element root = doc.getRootElement();
// System.out.println("Root: " + root.getName());
List projects = root.selectNodes("MasterData/Field");
// System.err.println(projects.size());
Iterator it = projects.iterator();
while (it.hasNext()) {
Element elm = (Element) it.next();
// System.out.println("index:"+elm.attributeValue("index")+" level:"+elm.attributeValue("level")+" nickname:"+elm.attributeValue("nickname")+" country:"+elm.attributeValue("country")+" weiwang:"+elm.attributeValue("weiwang"));
// System.err.println(elm.attributeValue(elm.attribute(0).getName()));
// System.err.println(elm.getTextTrim());
String text = elm.getTextTrim();
if (StringUtils.isNotBlank(text)) {
String[] split = text.split(" ");
for (String string : split) {
//放入数组省略
System.err.println(string);
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询