文人骚客2077
2014-12-27 16:43
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book id="1"> <name>冰与火之歌</name> <author>乔治马丁</author> <language>English</language> <price>109.9</price> </book> <book id="2"> <name>安徒生童话</name> <author>安徒生</author> <language>Chinese</language> <price>59.0</price> </book> </bookstore> /* 想在上面的xml内容中在加入一个book子节点,我应该怎么做? */
至于通过代码增加结点,可以参照:http://www.imooc.com/qadetail/66706
@da丶萌萌哒
如果是DOM4J的话,可以采用这种PrettyPrint
@da丶萌萌哒
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="1">
<name>冰与火之歌</name>
<author>乔治马丁</author>
<language>English</language>
<price>109.9</price>
</book>
<book id="2">
<name>安徒生童话</name>
<author>安徒生</author>
<language>Chinese</language>
<price>59.0</price>
</book>
<book id="3">
<name>第三本书</name>
<author>某某某</author>
<language>Chinese</language>
<price>444</price>
</book>
</bookstore>
这样就增加了一本书。
换行敲回车键,缩进的话敲空格或者tab键都可以哦~!
package com.imooc.domtest.test; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Scanner; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; public class CreateXML { public DocumentBuilder getDocumentBuilder(){ // 创建一个DocumentBuilderFactory的对象 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 创建DocumentBuilder对象 DocumentBuilder db =null; try { db = dbf.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); } return db; } /** * 生成xml */ public void createXML(){ Scanner in =new Scanner(System.in); DocumentBuilder db = getDocumentBuilder(); Document document = db.newDocument(); document.setXmlStandalone(true); Element bookstore = document.createElement("bookStore"); System.out.print("创建多少本书?:"); //向bookstore根节点中添加子节点book int n=in.nextInt(); for(int i=0;i<n;i++){ ArrayList<Element> elements=new ArrayList<Element>(); Element book = document.createElement("book"); Element name = document.createElement("name"); Element price = document.createElement("price"); Element language = document.createElement("language"); Element year = document.createElement("year"); elements.add(name); elements.add(price); elements.add(language); elements.add(year); // name.setNodeValue("小王子"); System.out.println("---------第"+(i+1)+"本书--------"); System.out.println("input the "+(i+1)+" book id"); String id=in.next(); book.setAttribute("id", id); for(int j=0;j<elements.size();j++){ System.out.println("input the"+(i+1)+" book "+elements.get(j).getNodeName()); String str=in.next(); elements.get(j).setTextContent(str); book.appendChild(elements.get(j)); } //将book节点添加到bookstore根节点中 bookstore.appendChild(book); } //将bookstore节点(已经包含了book)添加到dom树中 document.appendChild(bookstore); //创建TransformerFactory对象 TransformerFactory tff = TransformerFactory.newInstance(); try { //创建Transformer对象 Transformer tf = tff.newTransformer(); tf.setOutputProperty(OutputKeys.INDENT, "yes"); tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); tf.transform(new DOMSource(document),new StreamResult(new File("books1.xml"))); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } } public static void main(String[] args) { CreateXML creatXml = new CreateXML(); creatXml.createXML(); } }
```
package com.imooc.domtest.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class CreateXML {
public DocumentBuilder getDocumentBuilder(){
// 创建一个DocumentBuilderFactory的对象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// 创建DocumentBuilder对象
DocumentBuilder db =null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return db;
}
/**
* 生成xml
*/
public void createXML(){
Scanner in =new Scanner(System.in);
DocumentBuilder db = getDocumentBuilder();
Document document = db.newDocument();
document.setXmlStandalone(true);
Element bookstore = document.createElement("bookStore");
System.out.print("创建多少本书?:");
//向bookstore根节点中添加子节点book
int n=in.nextInt();
for(int i=0;i<n;i++){
ArrayList<Element> elements=new ArrayList<Element>();
Element book = document.createElement("book");
Element name = document.createElement("name");
Element price = document.createElement("price");
Element language = document.createElement("language");
Element year = document.createElement("year");
elements.add(name);
elements.add(price);
elements.add(language);
elements.add(year);
// name.setNodeValue("小王子");
System.out.println("---------第"+(i+1)+"本书--------");
System.out.println("input the "+(i+1)+" book id");
String id=in.next();
book.setAttribute("id", id);
for(int j=0;j<elements.size();j++){
System.out.println("input the"+(i+1)+" book "+elements.get(j).getNodeName());
String str=in.next();
elements.get(j).setTextContent(str);
book.appendChild(elements.get(j));
}
//将book节点添加到bookstore根节点中
bookstore.appendChild(book);
}
//将bookstore节点(已经包含了book)添加到dom树中
document.appendChild(bookstore);
//创建TransformerFactory对象
TransformerFactory tff = TransformerFactory.newInstance();
try {
//创建Transformer对象
Transformer tf = tff.newTransformer();
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
tf.transform(new DOMSource(document),new StreamResult(new File("books1.xml")));
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
CreateXML creatXml = new CreateXML();
creatXml.createXML();
}
}
```
Java眼中的XML 文件写入
55798 学习 · 132 问题
相似问题