首页>代码>Jdom、Dom4j、W3c、String相互转换大全以及取Xml的属性值、设置Xml的属性值、删除Xm属性值 >/XML/src/com/zhangjun/xml/XmlChange.java
                
                package com.zhangjun.xml;
import java.io.CharArrayReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xpath.XPathAPI;
import org.jdom.input.DOMBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class XmlChange {
	/**
	 * 将document对象,转换成字符串数据
	 * @param dom
	 * @return
	 */
	public static String dom2String(Document dom)
	// 将document对象,转换成字符串数据。
	{
		String aa = new String();
		try {
			StringWriter ss = new StringWriter();
			OutputFormat format = new OutputFormat(dom); // Serialize DOM
			format.setEncoding("GB2312");
			XMLSerializer serial = new XMLSerializer(ss, format);
			serial.asDOMSerializer(); // As a DOM Serializer
			serial.serialize(dom.getDocumentElement());
			aa = ss.toString();
			ss.flush();
			ss.close();
		} catch (Exception e) {
			// return false;
		}
		return aa;
	}
	/**
	 * 字符串转换成document
	 * 
	 * @param XMLData
	 * @return
	 */
	public static Document string2Dom(String XMLData)
	// 解析字符串xml数据,生成document
	{
		// System.out.println("xml:"+XMLData);
		Document dom = new DocumentImpl();
		try {
			InputSource source = new InputSource(new CharArrayReader(XMLData
					.toCharArray()));
			System.out.println("-----888888==" + source);
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			dom = docBuilder.parse(source);
			System.out.println("-----888888==" + dom);
		} catch (Exception e) {
			dom = null;
		} finally {
			return dom;
		}
	}
	/**
	 * 解析字符串xml数据,生成document,过滤xml中间的空格
	 * 
	 * @param data
	 * @param isSpace
	 * @return
	 * @throws IOException
	 * @throws SAXException
	 */
	public static Document string2Dom(String data, boolean isSpace)
			throws IOException, SAXException
	// 解析字符串xml数据,生成document,过滤xml中间的空格
	{
		if (isSpace) {
			DOMParser parser = new DOMParser();
			StringReader sr = new StringReader(data);
			InputSource is = new InputSource(sr);
			parser.parse(is);
			sr.close();
			return parser.getDocument();
		} else {
			return null;
		}
	}
	/**
	 * 实现dom4j向org.w3c.dom.Document的转换
	 * 
	 * @param doc
	 * @return
	 * @throws Exception
	 */
	public static org.w3c.dom.Document dom4j2W3c(Document doc) throws Exception {
		if (doc == null) {
			return (null);
		}
		java.io.StringReader reader = new java.io.StringReader(doc.toString());
		org.xml.sax.InputSource source = new org.xml.sax.InputSource(reader);
		javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory = javax.xml.parsers.DocumentBuilderFactory
				.newInstance();
		javax.xml.parsers.DocumentBuilder documentBuilder = documentBuilderFactory
				.newDocumentBuilder();
		return (documentBuilder.parse(source));
	}
	/**
	 * 
	 * 实现 org.w3c.dom.Document到dom4j的转换
	 * 
	 * @param doc
	 * @return
	 * @throws Exception
	 */
	public static org.dom4j.Document parse(org.w3c.dom.Document doc)
			throws Exception {
		if (doc == null) {
			return (null);
		}
		org.dom4j.io.DOMReader xmlReader = new org.dom4j.io.DOMReader();
		return (xmlReader.read(doc));
	}
	/**
	 * 
	 * 实现 org.w3c.dom.Document到jdom的转换
	 * 
	 * @param doc
	 * @return
	 * @throws Exception
	 */
	public static org.jdom.Document convertToJDOM(org.w3c.dom.Document doc)
			throws Exception {
		if (doc == null) {
			return (null);
		}
		DOMBuilder builder = new DOMBuilder();
		org.jdom.Document jdomDoc = builder.build(doc);
		return jdomDoc;
	}
	/**
	 * 根据tagName,attributeName从xml中取得取得相应Node属性值 可能有多个tagName定义的Node,取第一个
	 * 
	 * @param dom
	 * @param tagName
	 * @attributeName
	 * @return Attribute;
	 */
	public static String getAttributeValueFromDom(Document dom, String tagName,
			String attributeName) {
		String result = "";
		if (dom != null && tagName != null && !tagName.trim().equals("")
				&& attributeName != null && !attributeName.trim().equals("")) {
			NodeList nodeList = null;
			Node node = null;
			nodeList = dom.getElementsByTagName(tagName.trim());
			if (nodeList != null && nodeList.getLength() > 0) {
				/**
				 * xml中可能有多个tagName定义的Node,取第一个
				 */
				node = nodeList.item(0);
				if (node != null) {
					result = ((Element) node).getAttribute(attributeName);
					result = result == null ? "" : result.trim();
				}
			}
		}
		return result;
	}
	/**
	 * 设置xml节点属性值
	 * 
	 * @param dom
	 * @param tagName
	 * @param attributeName
	 * @param value
	 * @return
	 */
	public static boolean setAttributeValueFromDom(Document dom,
			String tagName, String attributeName, String value) {
		boolean result = false;
		if (dom != null && tagName != null && !tagName.trim().equals("")
				&& attributeName != null && !attributeName.trim().equals("")) {
			NodeList nodeList = null;
			Node node = null;
			nodeList = dom.getElementsByTagName(tagName.trim());
			if (nodeList != null && nodeList.getLength() > 0) {
				/**
				 * xml中可能有多个tagName定义的Node,取第一个
				 */
				node = nodeList.item(0);
				if (node != null) {
					((Element) node).setAttribute(attributeName, value);
					result = true;
				}
			}
		}
		return result;
	}
	/**
	 * 根据tagName从xml中取得取得相应Node值
	 * 
	 * @param dom
	 * @param tagName
	 * @return
	 */
	public static String getNodeValueFromDom(Document dom, String tagName) {
		String result = "";
		try {
			if (dom != null && tagName != null && !tagName.trim().equals("")) {
				NodeList nodeList = null;
				Node node = null;
				nodeList = XPathAPI.selectNodeList(dom, tagName.trim());// dom.getElementsByTagName(tagName.trim());
				if (nodeList != null && nodeList.getLength() > 0) {
					/**
					 * xml中可能有多个tagName定义的Node,取第一个
					 */
					node = nodeList.item(0);
					if (node != null) {
						node = node.getFirstChild();
						if (node != null)
							result = node.getNodeValue();
						result = result == null ? "" : result.trim();
					}
				}
			}
		} catch (Exception e) {
			System.out.println(" getNodeValueFromDom 出错 = " + e.toString());
			e.printStackTrace();
		}
		return result;
	}
	public static String getTagValueByTagName(Document dom, String tagName) {
		String result = "";
		try {
			if (dom != null && tagName != null && !tagName.trim().equals("")) {
				NodeList nodeList = null;
				Node node = null;
				if (dom.getElementsByTagName(tagName.trim()) != null) {
					nodeList = dom.getElementsByTagName(tagName.trim());
					if (nodeList.item(0) != null) {
						node = nodeList.item(0);
						if ((node.getChildNodes()).item(0).getNodeValue() != null) {
							result = (node.getChildNodes()).item(0)
									.getNodeValue().trim();
						}
					}
				}
			}
		} catch (Exception e) {
			System.out.println(" getNodeValueFromDom 出错 = " + e.toString());
			e.printStackTrace();
		}
		return result;
	}
}
最近下载更多
                
                1358849392     LV21
                2022年11月11日
            
            
        
                raccoonxx     LV2
                2020年6月4日
            
            
        
                放开那女孩     LV15
                2017年9月20日
            
            
        
                yl1998     LV17
                2017年3月13日
            
            
        
                eko     LV7
                2014年11月18日
            
            
        
                ysj123688     LV2
                2014年9月9日
            
            
        
                tianzhipeng     LV4
                2014年8月22日
            
            
        
                宋鲁妮     LV2
                2014年5月27日
            
            
        
                骑着猪猪去逛街     LV32
                2013年12月27日
            
            
        
                易拉罐     LV8
                2013年6月28日
            
            
        
最近浏览更多
                
                1358849392     LV21
                2022年11月11日
            
            
        
                maki666     LV1
                2021年4月15日
            
            
        
                幻羽揚     LV4
                2021年3月18日
            
            
        
                15939671505    
                2020年12月18日
            
            
                    暂无贡献等级
            
        
                raccoonxx     LV2
                2020年6月4日
            
            
        
                xiaoxiaosheep     LV6
                2020年4月3日
            
            
        
                hjy100200     LV6
                2020年3月4日
            
            
        
                JerryCoder    
                2019年11月13日
            
            
                    暂无贡献等级
            
        
                youye194     LV1
                2019年10月23日
            
            
        
                07311514     LV10
                2019年2月22日
            
            
        
                
                
    