0

我想将一些节点添加到现有的 xml 文件中。xml 文件结构如下所示:

<?xml version="1.0" encoding="utf-8" standalone="no"?><?xml-stylesheet type="text/xsl" href="new2.xsl"?><patients>
    <patient>
    <stoixeia_astheni>
        <arithmos_eksetasis>1</arithmos_eksetasis>
        <imerominia_eksetasis>11/12/2005</imerominia_eksetasis>
        <amka>14385</amka>
        <surname>aaa</surname>
        <name>aaa</name>
        <onoma_patros>aaa</onoma_patros>
        <imgennisis>15/03/1984</imgennisis>
        <diagnosi>aaa</diagnosi>
        <famagogi>depon</famagogi>
    </stoixeia_astheni>
    <stoixeia_epikoinonias>
        <dieuthinsi>aaa</dieuthinsi>
        <takodikas>11474</takodikas>
        <perioxi>aaa</perioxi>
        <stathero>2106425246</stathero>
        <kinito>-</kinito>
    </stoixeia_epikoinonias>
    <loipa_stoixeia>
        <fylo>aaa</fylo>
        <oikkatastasi>aaa</oikkatastasi>
        <epaggelma>aaa</epaggelma>
        <istoriko>-</istoriko>
        <sxolia>-</sxolia>
    </loipa_stoixeia>
    </patient>
    <patient>
    <stoixeia_astheni>
        <arithmos_eksetasis>2</arithmos_eksetasis>
        <imerominia_eksetasis>12/12/2005</imerominia_eksetasis>
        <amka>14325</amka>
        <surname>aaa</surname>
        <name>aaa</name>
        <onoma_patros>aaa</onoma_patros>
        <imgennisis>15/03/1984</imgennisis>
        <diagnosi>aaa</diagnosi>
        <famagogi>depon</famagogi>
    </stoixeia_astheni>
    <stoixeia_epikoinonias>
        <dieuthinsi>aaa</dieuthinsi>
        <takodikas>11474</takodikas>
        <perioxi>aaa</perioxi>
        <stathero>2106425246</stathero>
        <kinito>-</kinito>
    </stoixeia_epikoinonias>
    <loipa_stoixeia>
        <fylo>aaa</fylo>
        <oikkatastasi>aaa</oikkatastasi>
        <epaggelma>aaa</epaggelma>
        <istoriko>-</istoriko>
        <sxolia>-</sxolia>
    </loipa_stoixeia>
    </patient>
</patients>

我正在尝试类似的东西:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String[] elem;
    elem = new String[20];
    elem [1]="arithmos_eksetasis";
    elem [2]="imerominia_eksetasis";
    elem [3]="amka";
    elem [4]="surname";
    elem [5]="name";
    elem [6]="onoma_patros";
    elem [7]="imgennisis";
    elem [8]="diagnosi";
    elem [9]="famagogi";
    elem [10]="dieuthinsi";
    elem [11]="takodikas";
    elem [12]="perioxi";
    elem [13]="stathero";
    elem [14]="kinito";
    elem [15]="fylo";
    elem [16]="oikkatastasi";
    elem [17]="epaggelma";
    elem [18]="istoriko";
    elem [19]="sxolia";

    try {
        String filepath = "C:\\Users\\Chris\\Desktop\\tsiou\\workspace\\askhsh3\\WebContent\\askisi3.xml";

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(filepath);


        //Node staff = doc.getElementsByTagName("staff").item(0);


        // append a new node to staff
        Document doc2 = docBuilder.newDocument();
        Element patient = doc2.createElement("patient");

        Element st_as = doc2.createElement("stoixeia_astheni");
        for(int i=1;i<=9;i++){
            Element tmp= doc2.createElement(elem[i]);
            tmp.appendChild(doc2.createTextNode("aaa"));
            st_as.appendChild(tmp);
        }
        Element st_ep = doc2.createElement("stoixeia_epikoinonias");
        for(int i=10;i<=15;i++){
            Element tmp= doc2.createElement(elem[i]);
            tmp.appendChild(doc2.createTextNode("aaa"));
            st_ep.appendChild(tmp);     
        }
        Element st_lp = doc2.createElement("loipa_stoixeia");
        for(int i=16; i<=19;i++){
            Element tmp= doc2.createElement(elem[i]);
            tmp.appendChild(doc2.createTextNode("aaa"));
            st_lp.appendChild(tmp);     
        }
        patient.appendChild(st_as);
        patient.appendChild(st_ep);
        patient.appendChild(st_lp);
        doc.importNode(patient,true);

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        OutputStream out = new FileOutputStream(filepath);
        trans.transform(new DOMSource(doc), new StreamResult(out));

       } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
       } catch (TransformerException tfe) {
        tfe.printStackTrace();
       } catch (IOException ioe) {
        ioe.printStackTrace();
       } catch (SAXException sae) {
        sae.printStackTrace();
       }


    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();

}

结果没有运气(xml没有改变)。

当我尝试时trans.transform(new DOMSource(patient), new StreamResult(out));,我可以看到新患者..我也尝试过doc.adoptNode(patient)

任何建议都会很有用

4

1 回答 1

0

代替doc.importNode(patient,true);,试试这个:

//Find existing parent node
Node patientsNode = doc.getElementsByTagName("patients").item(0);

//append the new child node
patientsNode.appendChild(patient); 

我正在使用类似的代码,并且效果很好。

于 2013-05-28T14:44:26.423 回答