note : i am dealing with xml files that i don't know their structure in advance so i can't use XPath here is an example of my xml files :
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?>
<HWData>
<Header time="2013-05-29T13:39:34" uploaded="true" version="1.0" />
<NE vendorName="Nokia Siemens Networks" NEId="WBTS-431">
<EQHO vendorName="Nokia Siemens Networks" equipmentHolderId="173" >
<UNIT vendorName="N" unitId="16" />
<UNIT vendorName="NOKIA SIEMENS NETWORKS" unitId="225" />
</EQHO>
<EQHO vendorName="NSN" equipmentHolderId="40192" >
<UNIT vendorName="AR" unitId="40267" />
</EQHO>
</NE>
<NE vendorName="Nokia Siemens Networks" NEId="WBTS-261">
<EQHO vendorName="Nokia Siemens Networks" equipmentHolderId="132" >
<EQHO vendorName="Nokia Siemens Networks" equipmentHolderId="132-1">
<UNIT vendorName="NN" unitId="1621" />
</EQHO>
</EQHO>
</NE>
</HWData>
i want to know if i can use for example node name "NE" and attribute value NEID="WBTS-261" to have the hole as result!
result expected :
<NE vendorName="Nokia Siemens Networks" NEId="WBTS-261">
<EQHO vendorName="Nokia Siemens Networks" equipmentHolderId="132" >
<EQHO vendorName="Nokia Siemens Networks" equipmentHolderId="132-1">
<UNIT vendorName="NN" unitId="1621" />
</EQHO>
</EQHO>
</NE>
can someone put me on the right way to do that any example,idea or suggestion will be appreciated.. thank you
use this method to the node as string :
public static String NodeToString(String file,String moid) throws SAXException, IOException, ParserConfigurationException{
String stringNode="";
InputStream in = new ByteArrayInputStream(file.getBytes("ISO-8859-1"));
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
NodeList nodeList = doc.getElementsByTagName("*");
for (int i = 0; i < nodeList.getLength(); i++) {
if(nodeList.item(i) instanceof Element && ((Element)nodeList.item(i)).getAttribute("MOID").equalsIgnoreCase(moid)){
try
{
// Set up the output transformer
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
// Print the DOM node
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(nodeList.item(i));
trans.transform(source, result);
stringNode = sw.toString();
//System.out.println(xmlString);
}
catch (TransformerException e)
{
e.printStackTrace();
}
}
}
return stringNode;
}
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.