2017-11-27 13 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> 

を使用した:私は、1つまたは複数の新しい子ノードを挿入しようとしています

は、XMLファイルは次のようになりますように、それぞれに(数はアプリ、重要ではないに関連する検索結果に依存します)私はherehereherehereを見つけたものを私は一緒にこのコードを入れている:私は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ファイルがまったく更新されない - それは常に空白です。私は何時間も繰り返しこの頭を叩いていたにもかかわらず、なぜ、理解するのに苦労しています。

ご提案/ヒントには非常に感謝しています。多くの試行錯誤の後

答えて

0

次の変更で

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); 
     } 

結果を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> 
関連する問題