日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

w3c dom操作Xml时从加入另一个XML文件的一个元素

發(fā)布時間:2024/4/14 asp.net 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 w3c dom操作Xml时从加入另一个XML文件的一个元素 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

同時讀取兩個XML文件,如果需要把一個XML 文件中的某個特定元素的內容插入到另外一個XML文件中時,要用importNode()方法,具體使用例子如下:

?

import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map;import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory;import org.apache.commons.lang.StringUtils; import org.apache.tools.ant.Task; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import org.xml.sax.SAXException;/*@author:duanqf*/ public class MergeBeanRefContext {private static Element sourceRoot;private static Element destRoot;private static Document sourceDoc;private static Document destDoc;public static void merge(String sourceXmlPath, String destXmlPath, Task task) {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();try {factory.setIgnoringElementContentWhitespace(true);DocumentBuilder db = factory.newDocumentBuilder();db.setEntityResolver(new EntityResolver() {public InputSource resolveEntity(String publicId,String systemId) throws SAXException, IOException {if (publicId!=null && publicId.equals("-//SPRING//DTD BEAN//EN")) {return new InputSource(new ByteArrayInputStream("<?xml version='1.0' encoding='GBK'?>".getBytes())); }return null;}});sourceDoc = db.parse(new File(sourceXmlPath));sourceRoot = sourceDoc.getDocumentElement();destDoc = db.parse(new File(destXmlPath));destRoot = destDoc.getDocumentElement();execute(sourceDoc, destDoc, sourceRoot, destRoot, destXmlPath, task);} catch (Exception e) {e.printStackTrace();}}private static void execute(Document sourceDoc, Document destDoc,Element sourceRoot, Element destRoot, String destXmlPath, Task task) {Node constructorListNode = ParseXmlTools.selectSingleNode("/beans/bean[@id='qfContext']/constructor-arg/list",destRoot);NodeList destList = ParseXmlTools.selectNodes("value",constructorListNode);NodeList sourceList = ParseXmlTools.selectNodes("/beans/bean[@id='qfContext']/constructor-arg/list/value",sourceRoot);Map destPatternsMap = new HashMap();for (int j = 0; j < destList.getLength(); j++) {Node value = destList.item(j);if (value != null) {String valueStr = value.getTextContent();if (StringUtils.isNotBlank(valueStr)) {valueStr = valueStr.trim();destPatternsMap.put(valueStr, value);}}}for (int j = 0; j < sourceList.getLength(); j++) {Node value = sourceList.item(j);if (value != null) {String valueStr = value.getTextContent();if (destPatternsMap.get(valueStr) == null) {Node temp = destDoc.importNode(value, true);constructorListNode.appendChild(temp);// task.log("Add qfContext bean constructor-arg value:"// + valueStr);}}}//ParseXmlTools.saveXml(destXmlPath, destDoc);}

?

總結

以上是生活随笔為你收集整理的w3c dom操作Xml时从加入另一个XML文件的一个元素的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。