我想將一些節點添加到現有的xml文件中。 的xml文件的結構如下:java dom修改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));
我可以看到新的patient..i還試圖doc.adoptNode(patient)
任何意見將是有用的
看看JaxB和谷歌如何編組和解組一個對象。應該讓生活更輕鬆。 – Dan