JAVA解析xml得到节点的值
<MdmRecords><RecordrecordId="R546446"><FieldisMultiValue="0"><Name>TradeNo</Name><Val...
<MdmRecords>
<Record recordId="R546446">
<Field isMultiValue="0">
<Name>TradeNo</Name>
<Value>1001000082</Value>
</Field>
<Field isMultiValue="0">
<Name>TradeSort</Name>
<Value>03 客户</Value>
</Field>
<Field isMultiValue="0">
<Name>ProcessRelatedInfo</Name>
<Value>程>>数据编码平台>2015-08-05 14:23 3509814</Value>
</Field>
<Field isMultiValue="1">
<Name>SystemNoList_Customer</Name>
<Value/>
</Field>
<Field isMultiValue="1">
<Name>RequirementLocation</Name>
<Value/>
</Field>
<Field isMultiValue="1">
<Name>tblMasterData_Publish_Vendor</Name>
<Value>
<LookupRecord>
<Field isMultiValue="0">
<Name>SystemID</Name>
<Value>220CDF, 工建</Value>
</Field>
</LookupRecord>
</Value>
</Field>
<Field isMultiValue="1">
<Name>tblMasterData_Publish_Customer</Name>
<Value>
<LookupRecord>
<Field isMultiValue="0">
<Name>SystemID</Name>
<Value>220CDF, 工程建设</Value>
</Field>
</LookupRecord>
</Value>
</Field>
<Field isMultiValue="1">
<Name>tblTrade_Bank_Customer</Name>
<Value/>
</Field>
<Field isMultiValue="1">
<Name>tblTrade_Bank_Vendor</Name>
<Value/>
</Field>
<Field isMultiValue="1">
<Name>tblTrade_ContactPerson_Customer</Name>
<Value/>
</Field>
<Field isMultiValue="1">
<Name>tblTrade_ContactPerson_Vendor</Name>
<Value/>
</Field>
<Field isMultiValue="1">
<Name>tblTrade_SpecialSystemAttr</Name>
<Value>
<LookupRecord>
<Field isMultiValue="0">
<Name>SystemID</Name>
<Value>220CDF, 工程建</Value>
</Field>
<Field isMultiValue="0">
<Name>TradePartner</Name>
<Value/>
</Field>
</Record>
</MdmRecords>
如上的一个xml。我想获得每一个Field节点的Name中的值和对应的value值,value分割成数组取第一个的值,最好用org.w3c.dom解析。 展开
<Record recordId="R546446">
<Field isMultiValue="0">
<Name>TradeNo</Name>
<Value>1001000082</Value>
</Field>
<Field isMultiValue="0">
<Name>TradeSort</Name>
<Value>03 客户</Value>
</Field>
<Field isMultiValue="0">
<Name>ProcessRelatedInfo</Name>
<Value>程>>数据编码平台>2015-08-05 14:23 3509814</Value>
</Field>
<Field isMultiValue="1">
<Name>SystemNoList_Customer</Name>
<Value/>
</Field>
<Field isMultiValue="1">
<Name>RequirementLocation</Name>
<Value/>
</Field>
<Field isMultiValue="1">
<Name>tblMasterData_Publish_Vendor</Name>
<Value>
<LookupRecord>
<Field isMultiValue="0">
<Name>SystemID</Name>
<Value>220CDF, 工建</Value>
</Field>
</LookupRecord>
</Value>
</Field>
<Field isMultiValue="1">
<Name>tblMasterData_Publish_Customer</Name>
<Value>
<LookupRecord>
<Field isMultiValue="0">
<Name>SystemID</Name>
<Value>220CDF, 工程建设</Value>
</Field>
</LookupRecord>
</Value>
</Field>
<Field isMultiValue="1">
<Name>tblTrade_Bank_Customer</Name>
<Value/>
</Field>
<Field isMultiValue="1">
<Name>tblTrade_Bank_Vendor</Name>
<Value/>
</Field>
<Field isMultiValue="1">
<Name>tblTrade_ContactPerson_Customer</Name>
<Value/>
</Field>
<Field isMultiValue="1">
<Name>tblTrade_ContactPerson_Vendor</Name>
<Value/>
</Field>
<Field isMultiValue="1">
<Name>tblTrade_SpecialSystemAttr</Name>
<Value>
<LookupRecord>
<Field isMultiValue="0">
<Name>SystemID</Name>
<Value>220CDF, 工程建</Value>
</Field>
<Field isMultiValue="0">
<Name>TradePartner</Name>
<Value/>
</Field>
</Record>
</MdmRecords>
如上的一个xml。我想获得每一个Field节点的Name中的值和对应的value值,value分割成数组取第一个的值,最好用org.w3c.dom解析。 展开
2个回答
展开全部
import java.io.File;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ParseXml {
public static void main(String[] args) throws DocumentException {
SAXReader reader = new SAXReader();
Document doc = reader.read(new File("d:/test.xml"));
Element root = doc.getRootElement();
Element record = root.element("Record");
Iterator<Element> it = record.elementIterator("Field");
while (it.hasNext()) {
Element element = it.next();
Element name = element.element("Name");
System.out.println(name.getText());
Element value = element.element("Value");
ParseXml.parseValue(value);
}
}
public static void parseValue(Element value) {
Iterator<Element> it = value.elementIterator();
if (it.hasNext()) {
while (it.hasNext()) {
Element element = it.next();
Element innerField = element.element("Field");
Element innerName = innerField.element("Name");
System.out.println(innerName.getText());
Element innerValue = innerField.element("Value");
parseValue(innerValue);
}
} else {
System.out.println(value.getText());
}
}
}
2015-09-08
展开全部
org.w3c.dom.Node
有方法
String
getNodeName()
The name of this node, depending on its type; see the table above.
short
getNodeType()
A code representing the type of the underlying object, as defined above.
String
getNodeValue()
The value of this node, depending on its type; see the table above.
有方法
String
getNodeName()
The name of this node, depending on its type; see the table above.
short
getNodeType()
A code representing the type of the underlying object, as defined above.
String
getNodeValue()
The value of this node, depending on its type; see the table above.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询