繁体   English   中英

插入XML节点失败-Java DOM

[英]Insert XML Node Fail - Java DOM

Java和XML新手在这里转圈。 我的应用程序使用Powershell生成的XML文件作为源,然后将其添加到其他地方找到的数据中。

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文件如下所示:

<?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>

使用在这里这里这里这里找到的代码,我将这些代码放在一起:

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文件根本没有更新,因为我无法在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) {                            
                        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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM