2017-11-27 18 views
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); 
     } 

- 항상 빈입니다. 나는 왜 반복해서 머리를 몇 시간 동안 두드리는 데에도 불구하고 왜 이해해야하는지 고심하고있다.

모든 제안/팁에 대해 매우 고맙게 생각합니다.

답변

0

XML 파일

<?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.0"> 
      <CVE_ID>["2000-1081"]</CVE_ID> 
      <Description>Microsoft SQL Server 7.0/2000,Data Engine 1.0/2000 xp_displayparamstmt Buffer Overflow Vulnerability</Description> 
      <Source>ExploitDB</Source> 
      <CVE_URL>http://www.cvedetails.com/cve/CVE2000-1081/</CVE_URL> 
     </Exploit> 
    <Exploit ID="1.1"> 
      <CVE_ID>["2000-1083"]</CVE_ID> 
      <Description>Microsoft SQL Server 7.0/2000,Data Engine 1.0/2000 xp_showcolv Buffer Overflow Vulnerability</Description> 
      <Source>ExploitDB</Source> 
      <CVE_URL>http://www.cvedetails.com/cve/CVE2000-1083/</CVE_URL> 
     </Exploit> 
    <Exploit ID="1.2"> 
      <CVE_ID>["2000-1085"]</CVE_ID> 
      <Description>Microsoft SQL Server 7.0/2000,Data Engine 1.0/2000 xp_peekqueue Buffer Overflow Vulnerability</Description> 
      <Source>ExploitDB</Source> 
      <CVE_URL>http://www.cvedetails.com/cve/CVE2000-1085/</CVE_URL> 
     </Exploit> 
을 다음과 같이 변경에서 많은 시행 착오

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) {        
         Node childNode = childNodeList.item(1); 
         if (childNode.getNodeType() == Node.ELEMENT_NODE) { 
          Element eApp = (Element)childNode; 
          Attr eAttr = eApp.getAttributeNode("Name"); 
          String attrLabel = eAttr.getTextContent(); 
          if (attrLabel.equals("AppID")) { 
           String xmlAppID = eApp.getTextContent(); 
           if (xmlAppID.equals(appID)) { 
            System.out.println("AppID match found"); 
            Element newExploit = doc.createElement("Exploit"); 
            newExploit.setAttribute("ID", appID + "." + sequence.toString()); 
            node.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); 
     } 

결과 후