根元素不应重复,子节点仅需附加到XML Dom Parser中

时间:2018-10-10 14:21:53

标签: java xml-parsing domparser

提交表单后,值将存储在XML文件中。 (XML Dom PArser) 下面给出了我尝试做的程序。

public  void writeXMLfile(ApplyLieuDto lieuDto) {

    String satDte = 
    Util.convertUtilDateToString(lieuDto.getSatDutyDteUtil());
    //String satDteAMPM = lieuDto.getSatDutyDteAmPm();

    //String satDutyDte = satDte + satDteAMPM;

    String offDte = Util.convertUtilDateToString(lieuDto.getOffDteUtil());
    //String offDteAMPM = lieuDto.getOffDteAmPm();

    //String offDate = offDte + offDteAMPM;

    String modDate = 
     Util.convertUtilDateToString(lieuDto.getDateUpdateUtil());

    String filePath = "file.xml";


    try {
        DocumentBuilderFactory docFactory = 
         DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // root elements
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("lieu");
        doc.appendChild(rootElement);


        // staff elements
        Element staff = doc.createElement("staff");
        rootElement.appendChild(staff);

        // set attribute to staff element
        Attr attr = doc.createAttribute("id");
        attr.setValue(lieuDto.getStaffId());
        staff.setAttributeNode(attr);

        // name elements
        Element firstname = doc.createElement("name");
        firstname.appendChild(doc.createTextNode(lieuDto.getName()));
        staff.appendChild(firstname);

        // contact number elements
        Element contact = doc.createElement("contactnumber");
        contact.appendChild(doc.createTextNode(lieuDto.getContact()));
        staff.appendChild(contact);

        // email  elements
        Element email = doc.createElement("email");
        email.appendChild(doc.createTextNode(lieuDto.getEmail()));
        staff.appendChild(email);

        // satdutydate  elements
        Element satDutyDate = doc.createElement("satDte");
        satDutyDate.appendChild(doc.createTextNode(satDte));
        staff.appendChild(satDutyDate);

        // satdutydateAMPM  elements
        Element satDutyDateAMPM = doc.createElement("satDteAMPM");

        satDutyDateAMPM.appendChild(doc.createTextNode
        (lieuDto.getSatDutyDteAmPm()));
        staff.appendChild(satDutyDateAMPM);

        // offDate  elements
        Element offDat = doc.createElement("offdate");
        offDat.appendChild(doc.createTextNode(offDte));
        staff.appendChild(offDat);

        // offDateAMPM  elements
        Element offDatAMPM = doc.createElement("offdateAMPM");
        offDatAMPM.appendChild(doc.createTextNode(lieuDto.getOffDteAmPm()));
        staff.appendChild(offDatAMPM);

        // appOfficer  elements
        Element appOfficer = doc.createElement("approvingofficer");

       appOfficer.appendChild(doc.createTextNode
      (lieuDto.getApprovingOfficer()));
        staff.appendChild(appOfficer);

        // Date  elements
        Element modifieddate = doc.createElement("modifieddate");
        modifieddate.appendChild(doc.createTextNode(modDate));
        staff.appendChild(modifieddate);

        // status  elements
        Element status = doc.createElement("status");
        status.appendChild(doc.createTextNode(lieuDto.getStatus()));
        staff.appendChild(status);

        // write the content into xml file
        TransformerFactory transformerFactory = 
        TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new 
        FileWriter(filePath,true));

        // Output to console for testing
        StreamResult strmResult = new StreamResult(System.out);

        transformer.transform(source, result);
        transformer.transform(source, strmResult);


        System.out.println("File saved!");

    } catch (ParserConfigurationException e) {

        e.printStackTrace();
    } catch (TransformerConfigurationException e) {

        e.printStackTrace();
    } catch (TransformerException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }


}

我得到的输出如下所示

  <company>
<staff id="1">
    <firstname>Priya</firstname>
    <lastname>Rajan</lastname>
    <salary>100000</salary>
</staff>
</company>
<company>
<staff id="2">
    <firstname>Peter</firstname>
    <lastname>Jas</lastname>
    <salary>100000</salary>
</staff>
 </company>

但是,预期结果是

<company>
<staff id="1">
    <firstname>Priya</firstname>
    <lastname>Rajan</lastname>
    <salary>100000</salary>
</staff>

<staff id="2">
    <firstname>Peter</firstname>
    <lastname>Jas</lastname>
    <salary>100000</salary>
 </staff>
 </company>

根元素不应重复,子节点仅需附加。

请帮助我。

谢谢。

1 个答案:

答案 0 :(得分:0)

我得到了答案。 `

salq %cl, %rdx