課程
/后端開發(fā)
/Java
/Java眼中的XML 文件寫入
寫出來xml文件怎么自動縮進(jìn)啊,不僅僅是換行?還有怎么添加多個book節(jié)點(diǎn)啊?
2015-01-23
源自:Java眼中的XML 文件寫入 2-3
正在回答
縮進(jìn)可以采用tab鍵,也可以使用xml專門的編輯器;
添加多個book節(jié)點(diǎn):
<bookstore>
????<book>...</book>
????....
</bookstore>
腳踏地 提問者
JessicaJiang 回復(fù) 腳踏地 提問者
//之前之所以添加的book節(jié)點(diǎn)會覆蓋前面的book節(jié)點(diǎn),是因?yàn)樽兞棵枷嗤?,這樣的話自然下面的要替換上面的值
public static void main(String[] args) {??new TestXml().createXml();?}??public void createXml(){??DocumentBuilder db=getDocumentBuilder();??Document document=db.newDocument();??document.setXmlStandalone(true);??Element books=document.createElement("books");??document.appendChild(books);??//??添加多個節(jié)點(diǎn)??books.appendChild(getChildNode(document,"1","冰與火之歌","喬治馬丁","39"));??books.appendChild(getChildNode(document,"2","安徒生童話","安徒生","29"));????TransformerFactory tff=TransformerFactory.newInstance();????try {???Transformer tf=tff.newTransformer();???tf.setOutputProperty(OutputKeys.INDENT, "yes");
//設(shè)置縮進(jìn)量???tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");???tf.transform(new DOMSource(document), new StreamResult(new File("lib/books1.xml")));?????} catch (TransformerConfigurationException e) {???// TODO Auto-generated catch block???e.printStackTrace();??} catch (TransformerException e) {???// TODO Auto-generated catch block???e.printStackTrace();??}?}??public DocumentBuilder getDocumentBuilder(){??DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();??DocumentBuilder db = null;??try {???db=dbf.newDocumentBuilder();??} catch (ParserConfigurationException e) {???e.printStackTrace();??} ??return db;?}??public Element getChildNode(Document doc,String id,String name,String author,String price){??Element childNode=doc.createElement("book");??childNode.setAttribute("id", id);??childNode.appendChild(getChildNodeElement(doc,"name",name));??childNode.appendChild(getChildNodeElement(doc,"author",author));??childNode.appendChild(getChildNodeElement(doc,"price",price));??return childNode;?}
?public Element getChildNodeElement(Document doc,String name,String text){??Element element=doc.createElement(name);??element.setTextContent(text);??return element;?}
Raven1
Raven1 回復(fù) Raven1
舉報
舉例說明JAVA程序如何生成XML文檔,多種生成方式任你選擇
3 回答添加/修改XML文件節(jié)點(diǎn)
1 回答在DOM4J中怎么添加兩個相同的子節(jié)點(diǎn)?
5 回答換行但是沒有縮進(jìn)。。。這個要怎么辦? 如何在已有的xml文件中,再加一個book子節(jié)點(diǎn)?
1 回答添加子節(jié)點(diǎn)出現(xiàn)問題
1 回答怎么用dom方式向現(xiàn)有的xml文檔中添加節(jié)點(diǎn)
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2015-01-24
縮進(jìn)可以采用tab鍵,也可以使用xml專門的編輯器;
添加多個book節(jié)點(diǎn):
<bookstore>
????<book>...</book>
????<book>...</book>
????<book>...</book>
????....
</bookstore>
2015-01-27
//之前之所以添加的book節(jié)點(diǎn)會覆蓋前面的book節(jié)點(diǎn),是因?yàn)樽兞棵枷嗤?,這樣的話自然下面的要替換上面的值
public static void main(String[] args) {
??new TestXml().createXml();
?}
?
?public void createXml(){
??DocumentBuilder db=getDocumentBuilder();
??Document document=db.newDocument();
??document.setXmlStandalone(true);
??Element books=document.createElement("books");
??document.appendChild(books);
??
//??添加多個節(jié)點(diǎn)
??books.appendChild(getChildNode(document,"1","冰與火之歌","喬治馬丁","39"));
??books.appendChild(getChildNode(document,"2","安徒生童話","安徒生","29"));
??
??TransformerFactory tff=TransformerFactory.newInstance();
??
??try {
???Transformer tf=tff.newTransformer();
???tf.setOutputProperty(OutputKeys.INDENT, "yes");
//設(shè)置縮進(jìn)量
???tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
???tf.transform(new DOMSource(document), new StreamResult(new File("lib/books1.xml")));
???
??} catch (TransformerConfigurationException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??} catch (TransformerException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
?}
?
?public DocumentBuilder getDocumentBuilder(){
??DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
??DocumentBuilder db = null;
??try {
???db=dbf.newDocumentBuilder();
??} catch (ParserConfigurationException e) {
???e.printStackTrace();
??}
??return db;
?}
?
?public Element getChildNode(Document doc,String id,String name,String author,String price){
??Element childNode=doc.createElement("book");
??childNode.setAttribute("id", id);
??childNode.appendChild(getChildNodeElement(doc,"name",name));
??childNode.appendChild(getChildNodeElement(doc,"author",author));
??childNode.appendChild(getChildNodeElement(doc,"price",price));
??return childNode;
?}
?public Element getChildNodeElement(Document doc,String name,String text){
??Element element=doc.createElement(name);
??element.setTextContent(text);
??return element;
?}