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>
사용 : 내가 하나 개 이상의 새 자식 노드를 삽입하려고
은 XML 파일은 다음과 같이 수 있도록 각에 (숫자 앱, 중요하지 않은 관련 검색 결과에 따라 다름) 내가 here, here, here 내가 함께이 코드를 넣어 가지고 here을 발견 한 것을 :
내 XML 파일이 내가 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);
}
- 항상 빈입니다. 나는 왜 반복해서 머리를 몇 시간 동안 두드리는 데에도 불구하고 왜 이해해야하는지 고심하고있다.
모든 제안/팁에 대해 매우 고맙게 생각합니다.