DOM解析操作
只在跟节点<addresslist>下面建立一个子节点<name>
<?xml version="1.0" encoding="UTF-8"?> <addresslist> <linkman> <name>张三</name> <email>www.baidu.com</email> </linkman> <linkman> <name>李四</name> <email>www.sina.com</email> </linkman> </addresslist>
import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; //================================================= // File Name : DOM_demo //------------------------------------------------------------------------------ // Author : Common //类名:BinarySearch_Find //属性: //方法: //主类 //Function : DOM_demo public class DOM_demo { public static void main(String[] args) throws Exception{ // TODO 自动生成的方法存根 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //建立DocumentBuilderFactory DocumentBuilder builder = factory.newDocumentBuilder(); //建立DocumentBuilder Document doc = null; try{ doc = builder.parse("/home/common/software/coding/HelloWord/JavaWeb/bin/dom_name.xml"); }catch(SAXException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } NodeList nl = doc.getElementsByTagName("name"); //查找name节点 System.out.println("姓名:"+nl.item(1).getFirstChild().getNodeValue()); //输出第1个节点的内容 } }
一些DOM操作,循环输出节点信息
import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Element; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; //================================================= // File Name : DOM_demo //------------------------------------------------------------------------------ // Author : Common //类名:BinarySearch_Find //属性: //方法: //主类 //Function : DOM_demo public class DOM_demo { public static void main(String[] args) throws Exception{ // TODO 自动生成的方法存根 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //建立DocumentBuilderFactory DocumentBuilder builder = factory.newDocumentBuilder(); //建立DocumentBuilder Document doc = null; try{ doc = builder.parse("/home/common/software/coding/HelloWord/JavaWeb/bin/dom_name.xml"); }catch(SAXException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } // NodeList nl = doc.getElementsByTagName("name"); //查找name节点 // System.out.println("姓名:"+nl.item(1).getFirstChild().getNodeValue()); //输出第1个节点的内容 NodeList lm = doc.getElementsByTagName("linkman"); //查找linkman节点 for(int i=0;i<lm.getLength();i++){ Element e = (Element)lm.item(i); //取得每一个元素 System.out.println("姓名:"+e.getElementsByTagName("name").item(0).getFirstChild().getNodeValue()); System.out.println("邮箱:"+e.getElementsByTagName("email").item(0).getFirstChild().getNodeValue()); } } }
生成XML文件
import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; 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.xml.sax.SAXException; //================================================= // File Name : DOM_demo //------------------------------------------------------------------------------ // Author : Common //类名:BinarySearch_Find //属性: //方法: //主类 //Function : DOM_demo public class DOM_demo { public static void main(String[] args) throws Exception{ // TODO 自动生成的方法存根 //建立DocumentBuilderFactory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //建立DocumentBuilder DocumentBuilder builder = factory.newDocumentBuilder(); //定义Document接口对象,通过DocumentBuilder类进行DOM树的转换操作 Document doc = null; doc = builder.newDocument(); //建立各个操作节点 Element addresslist = doc.createElement("addresslist"); Element linkman = doc.createElement("linkman"); Element name = doc.createElement("name"); Element email = doc.createElement("email"); //设置节点的文本内容,即为每一个节点添加文本节点 name.appendChild(doc.createTextNode("王五")); email.appendChild(doc.createTextNode("www.soho.com")); //设置节点关系 linkman.appendChild(name); //子节点 linkman.appendChild(email); //子节点 addresslist.appendChild(linkman); //子节点 doc.appendChild(addresslist); //文档上保存节点 //输出文档到文件中 TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = null; try{ t = tf.newTransformer(); }catch(TransformerConfigurationException e1){ e1.printStackTrace(); } t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //设置编码 DOMSource source = new DOMSource(doc); //输出文档 StreamResult result = new StreamResult(new File("/home/common/software/coding/HelloWord/JavaWeb/bin/dom_name_output.xml")) ; try{ t.transform(source, result); //输出 }catch(TransformerException e){ e.printStackTrace(); } } }