使用dom4j修改XML格式的字符串

时间:2022-07-24
本文章向大家介绍使用dom4j修改XML格式的字符串,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

XML格式

<data>
    <ryzd>
        <record>
            <western>
                <record>
                    <diagnoses>
                        <record>
                            <indentation>0</indentation>
                            <content>颈后纵韧带骨化</content>
                            <allContent>颈后纵韧带骨化 2018-10-16 管理员</allContent>
                            <allContents>颈后纵韧带骨化 2018-10-16 管理员</allContents>
                            <diagName>颈后纵韧带骨化</diagName>
                            <indentTimes>1</indentTimes>
                            <diagAllTime>20181016150508</diagAllTime>
                            <outCome />
                            <content2>
                                <record>
                                    <content>颈后纵韧带骨化</content>
                                </record>
                            </content2>
                            <outCome2>
                                <record>
                                    <content />
                                </record>
                            </outCome2>
                        </record>
                    </diagnoses>
                </record>
            </western>
            <signature_ystime />
            <signature_ysname />
            <signature_ystitle>主任医师</signature_ystitle>
            <creator_name />
            <signer>管理员</signer>
            <signature_date>2018-10-16</signature_date>
            <signature_time>2018-10-16 15:05</signature_time>
            <signature_times>2018年10月16日</signature_times>
            <signerImage />
            <signature_alltime>20181016150508</signature_alltime>
            <signature_timess>2018年10月16日 15点05分</signature_timess>
            <diag_sn_list>15396735088300954413</diag_sn_list>
        </record>
    </ryzd>
    <cyzd>
        <record>
            <western>
                <record>
                    <diagnoses>
                        <record>
                            <indentation>0</indentation>
                            <content>颈后纵韧带骨化</content>
                            <allContent>颈后纵韧带骨化 2018-10-17 管理员</allContent>
                            <allContents>颈后纵韧带骨化 2018-10-17 管理员</allContents>
                            <diagName>颈后纵韧带骨化</diagName>
                            <indentTimes>1</indentTimes>
                            <diagAllTime>20181017144706</diagAllTime>
                            <outCome />
                            <content2>
                                <record>
                                    <content>颈后纵韧带骨化</content>
                                </record>
                            </content2>
                            <outCome2>
                                <record>
                                    <content />
                                </record>
                            </outCome2>
                        </record>
                        <record>
                            <indentation>0</indentation>
                            <content>胸椎骨折T11/T12</content>
                            <allContent>胸椎骨折T11/T12 2018-10-17 管理员</allContent>
                            <allContents>胸椎骨折T11/T12 2018-10-17 管理员</allContents>
                            <diagName>胸椎骨折T11/T12</diagName>
                            <indentTimes>1</indentTimes>
                            <diagAllTime>20181017144719</diagAllTime>
                            <outCome>好转</outCome>
                            <content2>
                                <record>
                                    <content>胸椎骨折T11/T12</content>
                                </record>
                            </content2>
                            <outCome2>
                                <record>
                                    <content>好转</content>
                                </record>
                            </outCome2>
                        </record>
                    </diagnoses>
                </record>
            </western>
            <signature_ystime />
            <signature_ysname />
            <signature_ystitle>主任医师</signature_ystitle>
            <creator_name />
            <signer>管理员</signer>
            <signature_date>2018-10-17</signature_date>
            <signature_time>2018-10-17 14:47</signature_time>
            <signature_times>2018年10月17日</signature_times>
            <signerImage />
            <signature_alltime>20181017144706</signature_alltime>
            <signature_timess>2018年10月17日 14点47分</signature_timess>
            <diag_sn_list>15397588260500045136,15397588396850046153
            </diag_sn_list>
        </record>
    </cyzd>
</data>

dom4j修改方法

private String getDiagXml(String diagXml, String postoperativeDiagnosis){
        // TODO Auto-generated method stub
        try {
            Document doc=DocumentHelper.parseText(diagXml);
            Element root = doc.getRootElement();
            Element cyzd = root.element("cyzd");
            Element diagnoses = cyzd.element("record").element("western").element("record").element("diagnoses");//获取要增加节点的父节点
            Element record = diagnoses.addElement("record");
            Element indentation = record.addElement("indentation");
            indentation.setText("0");
            Element content = record.addElement("content");
            content.setText(postoperativeDiagnosis);
            Element diagName = record.addElement("diagName");
            diagName.setText(postoperativeDiagnosis);
            Element indentTimes = record.addElement("indentTimes");
            indentTimes.setText("1");
            String date = new java.text.SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date()); //获取当前时间
            Element diagAllTime = record.addElement("diagAllTime");
            diagAllTime.setText(date);
            
            String newXml=doc.asXML();    //将修改后的内容组织成新的字符串
            return newXml;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return diagXml;
    }