Java操作xml源代码

h759502190 贡献于2013-10-09

作者 x  创建于2009-07-17 00:26:00   修改者ws  修改于2011-10-22 03:42:00字数4549

文档摘要:Java操作xml源代码
关键词:

import java.io.FileWriter; import java.io.IOException; import java.util.Iterator; import org.dom4j.*; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class Dom4jDmeo { static void readXML(String path){ SAXReader reader=new SAXReader();//构建sax读取对象 try { Document doc=reader.read(path);//获取文档对象 Element root=doc.getRootElement();//获取根节点 System.out.println("根节点:"+root.getName()); Iterator it=root.elementIterator();//获取根节点下面的所有子节点 while(it.hasNext()){ Element el=(Element) it.next();//遍历出根节点下面的单个节点 System.out.println(el.elementText("NO")); System.out.println(el.element("NO").attributeValue("DATE")); } } catch (DocumentException e) { e.printStackTrace(); } } static void findXML(String path){ SAXReader reader=new SAXReader();//构建sax读取对象 try { Document doc=reader.read(path);//获取文档对象 Element root=doc.getRootElement();//获取根节点 Node node=doc.selectSingleNode("//Result/VALUE/NO"); System.out.println(node.getText()); } catch (DocumentException e) { e.printStackTrace(); } } static void writeXML(){ //构建xml输出对象 try { XMLWriter writer=new XMLWriter(new FileWriter("d:/link.xml")); Document doc=DocumentHelper.createDocument();//创建文档对象 Element root=doc.addElement("学生信息"); root.addElement("学生").addElement("姓名").addAttribute("性别", "男").addText("张三");//构建子节点,属性值和显示文本 root.addElement("学生").addElement("姓名").addAttribute("性别", "女").addText("历史");//构建子节点,属性值和显示文本 writer.write(doc);//输出文档对象 writer.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { //Dom4jDmeo.readXML("d:/text.xml"); //Dom4jDmeo.writeXML(); Dom4jDmeo.findXML("d:/text.xml"); } } =====================================================================import java.io.*; import org.w3c.dom.*; import javax.xml.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.util.*; public class DomDemo { static void readXML(String path){ //构建文件对象 File file=new File(path); //构建文档对象工厂 DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); try { //构建文档对象 DocumentBuilder db=dbf.newDocumentBuilder(); Document dom=db.parse(file);//讲文件流转换为文档对象 //遍历文档节点列表 NodeList nodes=dom.getElementsByTagName("VALUE"); NodeList list=dom.getChildNodes();//获取说有子节点 for (int i = 0; i < list.getLength(); i++) { Node no=list.item(i); System.out.println(no.getChildNodes().getLength()); } System.out.println("========================"); for (int i = 0; i < nodes.getLength(); i++) { System.out.println(dom.getElementsByTagName("NO").item(i).getNodeName());//子节点名 System.out.println(dom.getElementsByTagName("NO").item(i).getAttributes().item(0).getNodeName());//子节点属性名 System.out.println(dom.getElementsByTagName("NO").item(i).getAttributes().item(0).getNodeValue());//子节点属性值 System.out.println(dom.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue());//子节点项值 System.out.println(dom.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue());//子节点项值 } } catch (Exception e) { e.printStackTrace(); } } static void writeXML(String path){ File file=new File(path); DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); try { DocumentBuilder db=dbf.newDocumentBuilder(); Document doc=db.newDocument(); //创建跟节点 Element root=doc.createElement("学生信息"); doc.appendChild(root); //创建子节点 Element e1=doc.createElement("学生"); e1.setAttribute("sex", "男"); root.appendChild(e1); //创建子节点项 Element ee1=doc.createElement("姓名"); e1.appendChild(ee1);//将项绑定到子节点上 Text name=doc.createTextNode("zhangsan"); ee1.appendChild(name); //创建子节点项 Element ee2=doc.createElement("年龄"); e1.appendChild(ee1);//将项绑定到子节点上 Text age=doc.createTextNode("20"); ee2.appendChild(age); //构建事务转换工厂 TransformerFactory tFactory =TransformerFactory.newInstance(); //构建转换器 Transformer transformer; try { transformer = tFactory.newTransformer(); Source source=new DOMSource(doc);//构建数据源 StreamResult result = new StreamResult(new java.io.File(path));//构建xml输出流 transformer.transform(source, result);//输出xml流文件 } catch (Exception e) { e.printStackTrace(); } } catch (ParserConfigurationException e) { e.printStackTrace(); } } public static void main(String[] args) { DomDemo.writeXML("d:/links.xml"); } } Subscriber:MaYong Subscription Code:ZLR8ZO-655444-54678656985359684 Subscriber:LauCheng Subscription Code:YLR8ZC-855550-6765665204902409 Subscriber:accp Subscription Code:nLR8ZC-855550-6765855499710139 Subscriber:Administrator Subscription Code:NLR8ZC-855550-6552685393219112 Subscriber:accptech Subscription Code:nLR8ZC-855550-6765855429037911

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 3 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档