0
Java & XMLの初心者はここでサークルを回ります。私のアプリは、PowerShellで生成されたXMLファイルをソースとして使用し、それ以外の場所にあるデータを充実させます。XMLノードの挿入に失敗 - Java DOM
ファイルの末尾に複数持つようにXMLファイル
<?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>
と、。
<?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>
を使用した:私は、1つまたは複数の新しい子ノードを挿入しようとしています
は、XMLファイルは次のようになりますように、それぞれに(数はアプリ、重要ではないに関連する検索結果に依存します)私はhere、here、hereとhereを見つけたものを私は一緒にこのコードを入れている:私はxmlAppIDに試合を取得することはできませんので
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);
}
私のXMLファイルがまったく更新されない - それは常に空白です。私は何時間も繰り返しこの頭を叩いていたにもかかわらず、なぜ、理解するのに苦労しています。
ご提案/ヒントには非常に感謝しています。多くの試行錯誤の後