java解析xml 克隆xml

来源:互联网 发布:磁力链接在线播放软件 编辑:程序博客网 时间:2024/05/17 07:24
1
package com.ilvyou.xml;
import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.util.Iterator;import java.util.List;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.Namespace;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;import org.xml.sax.SAXException;public class ReadXmlUtil {private Document document;public ReadXmlUtil(String path) {SAXReader reader = new SAXReader();try {reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);} catch (SAXException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {document = reader.read(new File(path));} catch (DocumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void filterXml() {Element rootElement = document.getRootElement();//得到头结点Iterator iterator = rootElement.elementIterator();//返回迭代子(包含子节点)Document createDocument = DocumentHelper.createDocument();Element createRoot = createDocument.addElement("urlset", rootElement.getNamespaceURI());//创建头结点while (iterator.hasNext()) {Element element = (Element) iterator.next();String locVal = element.elementTextTrim("loc");//得到名称为loc的节点内容、String lastmodVal = element.elementTextTrim("lastmod");//得到名称为lastmod的节点内容String changefreqVal = element.elementTextTrim("changefreq");//得到名称为changefreq的节点内容String priorityVal = element.elementTextTrim("priority");//得到名称为priorty的节点内容Element createUrl = createRoot.addElement("url");Element createLoc = createUrl.addElement("loc");createLoc.setText(locVal);Element createLastmod = createUrl.addElement("lastmod");createLastmod.setText(lastmodVal);Element createChangefreq = createUrl.addElement("changefreq");createChangefreq.setText(changefreqVal);Element createPriority = createUrl.addElement("priority");createPriority.setText(priorityVal);}try {OutputFormat format = OutputFormat.createPrettyPrint();format.setEncoding("utf-8");XMLWriter writer = new XMLWriter(new FileWriter(new File("d://sitemap1.xml")), format);writer.write(createDocument);writer.close();} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {String path = "e://sitemap1.xml";ReadXmlUtil readXmlUtil = new ReadXmlUtil(path);readXmlUtil.filterXml();}}

2

package com.ilvyou.xml;import java.io.File;import java.net.URL;import java.util.List;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.DocumentHelper;import org.dom4j.Node;import org.dom4j.io.SAXReader;import org.xml.sax.SAXException;public class XmlUtil {private Document document;public XmlUtil() {document = DocumentHelper.createDocument();}public XmlUtil(String filePath) {SAXReader reader = new SAXReader();try {reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",false);} catch (SAXException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {document = reader.read(new File(filePath));} catch (DocumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public String getAttributeValue(String strXPath) {Node n = document.selectSingleNode(strXPath);if (n != null) {return (n.valueOf("@value"));} else {return null;}}
public static void main(String[] args){String xmlPath = XmlUtil.class.getResource("/config.xml").toString();if (xmlPath.substring(5).indexOf(":") > 0) {// 路径中含有:分隔符,windows系统xmlPath = xmlPath.substring(6);} else {xmlPath = xmlPath.substring(5);}config = new XmlUtil(xmlPath);String a = config.getAttributeValue("//config/domain/item[@key='domain']"); String b = config.getAttributeValue("//config/sso/item[@key='host']");}





原创粉丝点击