I need to parse the below format xml and based on the values i have to generate new xml.
<transactionDataSection>
<FIELD name="VAR11" value="Subash"/>
<FIELD name="VAR10" value="India"/>
<FIELD name="VAR9" value="XXXX"/>
<FIELD name="VAR8" value="YYYY"/>
<TableDataSection>
<table>
<tableName>12345678</tableName>
<row>
<rowNumber>1</rowNumber>
<rowData>
<FIELD name="VAR1" value="Val1"/>
<FIELD name="VAR2" value="Val2"/>
<FIELD name="VAR3" value="Val3"/>
</rowData>
</row>
<row>
<rowNumber>2</rowNumber>
<rowData>
<FIELD name="VAR4" value="Val4"/>
<FIELD name="VAR5" value="Val5"/>
<FIELD name="VAR6" value="Val6"/>
</rowData>
</row>
</table>
</TableDataSection>
</transactionDataSection>
Here i am facing challenges to retrieve the field name and the values. Here i need the field name and values inside transaction data section separately and table data section separately to construct new xml. Already tried using DOM parser but it gives all the field name and values.
NodeList transactionDataSectionNodeList = document.getElementsByTagName("transactionDataSection");
for(int i=0;i<transactionDataSectionNodeList.getLength();i++){
Node transactionDataSectionNode = transactionDataSectionNodeList.item(i);
if(transactionDataSectionNode.getNodeType() == Node.ELEMENT_NODE){
Element eElement = (Element) transactionDataSectionNode;
NodeList fieldList = eElement.getChildNodes();
for(int y=0;y<fieldList.getLength();y++){
Node fieldNode = fieldList.item(y);
if(fieldNode.getNodeType() == Node.ELEMENT_NODE){
Element element=(Element) fieldNode;
if(fieldNode.getNodeName().equals("FIELD")){
Element field2 = doc.createElement("field");
Element name2 = doc.createElement("name");
Element value2 = doc.createElement("value");
/*System.out.println("name : "+element.getAttribute("name"));
System.out.println("value : "+element.getAttribute("value"));*/
name2.appendChild(doc.createTextNode(element.getAttribute("name")));
value2.appendChild(doc.createTextNode(element.getAttribute("value")));
field2.appendChild(name2);
field2.appendChild(value2);
Transaction_data.appendChild(field2);
}
}
}
}
}
But i want the field name inside the transaction data section separately and table data section separately. Can anyone please help?
Since
TableDataSection
is a child of
transactionDataSection
, when you do
Element eElement = (Element) transactionDataSectionNode;
NodeList fieldList = eElement.getChildNodes();
you get TableDataSection back as well.
What you can do is, for each child node have a check,
if(fieldNode.getNodeName().equals("TableDataSection"){
callXMLGenerationForTableDataSection(fieldNode);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.