将XML文件的节点拆分出来,求解决
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<Basicinfo>
<element name="InterfaceType">OrderSend</element>
<element name="InterfaceBuzType">BuzInter</element>
<element name="MsgSender">1111111111111</element>
<element name="MsgReceiver">11111111</element>
<element name="SendTime">20131211092749</element>
<element name="ResendTimes">0</element>
</Basicinfo>
<OrderInfo_item>
<element name="CenterManagerTel">00/1234567800</element>
<element name="CustGroup">2012020103</element>
<CircuitInfo_item_GLB>
<element name="AvailablitySla">107630002</element>
<element name="RequireTimeSla">2013-12-20</element>
<GLB_RouteInfo_item>
<element name="RouteOrderNbr">217</element>
<element name="CircuitID">106500221</element>
<element name="PlanNbr">1</element>
</GLB_RouteInfo_item>
<GLB_RouteInfo_item_B>
<element name="RouteOrderNbr">217</element>
<element name="CircuitID">106500221</element>
<element name="RouteType">1</element>
<element name="PlanNbr">1</element>
</GLB_RouteInfo_item_B>
</CircuitInfo_item_GLB>
</OrderInfo_item>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<root_Basicinfo>
<Basicinfo>
<element name="InterfaceType">OrderSend</element>
<element name="InterfaceBuzType">BuzInter</element>
<element name="MsgSender">1111111111111</element>
<element name="MsgReceiver">11111111</element>
<element name="SendTime">20131211092749</element>
<element name="ResendTimes">0</element>
</Basicinfo>
</root_Basicinfo>
<?xml version="1.0" encoding="UTF-8"?>
<root_OrderInfo_item>
<OrderInfo_item>
<element name="CenterManagerTel">00/1234567800</element>
<element name="CustGroup">2012020103</element>
</OrderInfo_item>
</root_OrderInfo_item>
<?xml version="1.0" encoding="UTF-8"?>
<root_CircuitInfo_item_GLB>
<CircuitInfo_item_GLB>
<element name="AvailablitySla">107630002</element>
<element name="RequireTimeSla">2013-12-20</element>
</CircuitInfo_item_GLB>
</root_CircuitInfo_item_GLB>
<?xml version="1.0" encoding="UTF-8"?>
<root_GLB_RouteInfo_item>
<GLB_RouteInfo_item>
<element name="RouteOrderNbr">217</element>
<element name="CircuitID">106500221</element>
<element name="PlanNbr">1</element>
</GLB_RouteInfo_item>
</root_GLB_RouteInfo_item>
<?xml version="1.0" encoding="UTF-8"?>
<root_GLB_RouteInfo_item_B>
<GLB_RouteInfo_item_B>
<element name="RouteOrderNbr">217</element>
<element name="CircuitID">106500221</element>
<element name="RouteType">1</element>
<element name="PlanNbr">1</element>
</GLB_RouteInfo_item_B>
</root_GLB_RouteInfo_item_B>
private List<String[]> analysisXml(Document doc) {
String type = "default";
@SuppressWarnings("unchecked")
List<Element> items = doc.selectNodes("/web-app/Basicinfo/element");
for (Element item : items) {
String name = item.attributeValue("name");
if ("InterfaceBuzType".equals(name)) {
type = item.getText();
break;
}
}
items.clear();
// 以二级节点为单位拆分报文
List<String[]> ret = new ArrayList<String[]>();
StringBuilder xmlStr = new StringBuilder(2000);
items = doc.getRootElement().elements();
for (Element element : items) {
xmlStr.setLength(0);
String name = element.getName();
xmlStr.append("<?xml version="1.0" encoding="");
xmlStr.append(CopyOfFactory.encoding);
xmlStr.append(""?>\n<root_");
xmlStr.append(name).append(">\n");
xmlStr.append(element.asXML());
xmlStr.append("\n</root_").append(name).append(">");
ret.add(new String[] { name, xmlStr.toString(), type });
}
items.clear();
return ret;
}
[解决办法]
百度一下DOM4J,解析XML例子。用DOM4J解析的话,很方便的
[解决办法]
不就是解析一下 然后封装?
[解决办法]
你就用dom4j把节点全部封装成对象。再把对象封装成你的xml
[解决办法]
dom4j 解析很实用,要是解析后封装成对象xstream 更实用。
[解决办法]
试试这个
public static void main(String[] args) {
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("路径/a.xml"));
Element root = document.getRootElement();
parse(root);
} catch (DocumentException ex) {
ex.printStackTrace(System.err);
}
}
public static void parse(Element element) {
List<Element> es = element.elements("element");
if (!es.isEmpty()) {
String name = element.getName();
StringBuilder builder = new StringBuilder();
builder.append("<?xml version="1.0" encoding="UTF-8"?>\r\n");
builder.append("<root_").append(name).append(">\r\n");
builder.append("\t<").append(name).append(">\r\n");
for (Element e : es) {
builder.append("\t\t").append(e.asXML()).append("\r\n");
}
builder.append("\t</").append(name).append(">\r\n");
builder.append("</root_").append(name).append(">\r\n");
System.out.println(builder.toString());
}
es = element.elements();
for (Element e : es) {
if (!e.getName().equals("element")) {
parse(e);
}
}
}