package demo; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; 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; import org.w3c.dom.Node; import org.xml.sax.SAXException; public class xmlTest { public static void main(String[] args) { //调用 DocumentBuilderFactory.newInstance() 方法得到创建 DOM 解析器的工厂 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); Element theBook=null, theElem=null, root=null; try { factory.setIgnoringElementContentWhitespace(true);//是否删除空格 false true DocumentBuilder db=factory.newDocumentBuilder(); //获取解析器 //指定路径 获取xml文件的document对象 File f = new File("src/book.xml"); Document xmldoc=db.parse(f); root=xmldoc.getDocumentElement(); //添加 元素(节点) theBook=xmldoc.createElement("book1"); theElem=xmldoc.createElement("name"); theElem.setTextContent("平凡的世界"); theBook.appendChild(theElem); theElem=xmldoc.createElement("price"); theElem.setTextContent("¥55.0"); theBook.appendChild(theElem); theElem=xmldoc.createElement("conment"); theElem.setTextContent("推荐大家看看这本书"); theBook.appendChild(theElem); root.appendChild(theBook); printXML(xmldoc); //打印至Console saveXml("new_book.xml", xmldoc);//保存生成 xml文件 } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 将node的XML字符串打印到控制台 * @param node */ public static void printXML(Node node) { TransformerFactory transFactory=TransformerFactory.newInstance(); try { Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty("encoding", "gb2312");//指定编码 transformer.setOutputProperty("indent", "yes"); //是否设置缩进 DOMSource source=new DOMSource(); source.setNode(node); StreamResult result=new StreamResult(); result.setOutputStream(System.out); transformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } } /** * 保存生成xml文件 * @param fileName * @param doc */ public static void saveXml(String fileName, Document doc) {//将Document输出到文件 TransformerFactory transFactory=TransformerFactory.newInstance(); try { Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty("indent", "yes");//是否设置缩进 DOMSource source=new DOMSource(); source.setNode(doc); StreamResult result=new StreamResult(); result.setOutputStream(new FileOutputStream(fileName)); transformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }


wbbhappy LV13
2024年1月11日
1443251642 LV1
2022年12月19日
zql666 LV9
2021年9月28日
壹级天灾 LV14
2021年7月7日
Hachi6 LV13
2021年5月10日
15939671505
2020年12月18日
暂无贡献等级
djzhang LV1
2020年11月5日
旧梦十年丶 LV2
2020年10月21日
1234567891011 LV5
2020年6月26日
guwuqifei LV5
2020年6月19日