Java & XML novice going around in circles here. My app uses a powershell generated XML file as a source, which it then enriches with data found elsewhere.
XML File
<?xml version="1.0" encoding="UTF-8"?>
-<Objects>
-<Object>
<Property Name="AppID">1</Property>
<Property Name="DisplayName">Adobe AIR</Property>
<Property Name="DisplayVersion">27.0.0.124</Property>
<Property Name="Publisher">Adobe Systems Incorporated</Property>
</Object>
and so on, with multiple to the end of the file.
I am trying to insert one or more new child nodes (the number depends on search result related to the app, not important) into each so that the XML file will look like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Objects>
<Object>
<Property Name="AppID">1</Property>
<Property Name="DisplayName">Adobe AIR</Property>
<Property Name="DisplayVersion">27.0.0.124</Property>
<Property Name="Publisher">Adobe Systems Incorporated</Property>
<Exploit ID="1.1">
<CVE_ID>2001-0001</CVE_ID>
<Description>text</Description>
<Source>CVE</CVE_ID>
<CVE_URL>http://www.....</CVE_URL>
</Exploit>
<Exploit ID="1.2">
<CVE_ID>2001-0001</CVE_ID>
<Description>text</Description>
<Source>CVE</CVE_ID>
<CVE_URL>http://www.....</CVE_URL>
</Exploit>
</Object>
Using what I've found here, here, here and here I have put together this code:
try {
//open the XML file for edit
DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
DocumentBuilder dB = dBF.newDocumentBuilder();
Document doc = dB.parse(new File(filename));
NodeList nodeList = doc.getElementsByTagName("Object");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element)node;
NodeList childNodeList = e.getChildNodes();
if (childNodeList.getLength() > 0) {
for (int j = 0 ; j < childNodeList.getLength() ; j++ ) {
Node xmlApp = childNodeList.item(j);
if (xmlApp.getNodeType() == Node.ELEMENT_NODE) {
Element eApp = (Element)xmlApp;
String xmlAppID = eApp.getAttribute("AppID");
if (xmlAppID.equals(appID)) {
System.out.println("AppID match found");
Element newExploit = doc.createElement("Exploit");
newExploit.setAttribute("ID", appID + "." + sequence.toString());
xmlApp.appendChild(newExploit);
Element newCVEID = doc.createElement("CVE_ID");
newCVEID.setTextContent(cve);
newExploit.appendChild(newCVEID);
Element newDescription = doc.createElement("Description");
newDescription.setTextContent(description);
newExploit.appendChild(newDescription);
Element newSource = doc.createElement("Source");
newSource.setTextContent(source);
newExploit.appendChild(newSource);
Element newCVEURL = doc.createElement("CVE_URL");
newCVEURL.setTextContent(cveURL);
newExploit.appendChild(newCVEURL);
}
}
}
}
}
}
Transformer tr = TransformerFactory.newInstance().newTransformer();
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.setOutputProperty(OutputKeys.METHOD, "xml");
tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
//tr.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, cat + ".dtd");
tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
// send DOM to file
tr.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(filename)));
} catch (SAXException | IOException | ParserConfigurationException | TransformerException ex) {
Logger.getLogger(reportClass.class.getName()).log(Level.SEVERE, null, ex);
}
My XML file is not updated at all because I can't get a match on xmlAppID - it's always blank. I'm struggling to understand why, despite banging my head off this repeatedly for hours.
I'd be very grateful for any suggestions/tips.