Java XML 简要读写工具

##这个工具类主要完成字符型XML的读取和把对象转换成XML的操作

工具类
package cn.net.jk.common.utils;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import bsh.This;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XMLHelper {
	private static Log logger = LogFactory.getLog(This.class); 
	XPath xpath = null;
	org.w3c.dom.Document doc = null;

	public XMLHelper(String xmlStr) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			doc = db.parse(new InputSource(new StringReader(xmlStr)));
			xpath = XPathFactory.newInstance().newXPath();
		} catch (Exception e) {
			logger.error(e);
		}

	}

	/**
	 * XML生成方法,可以把对象转换成XML 生成的根名称是类名
	 * 
	 * @param o
	 * @return
	 */
	public synchronized static String objectToXml(Object o) {
		String xml = "";
		try {
			XStream xStream = new XStream(new DomDriver());
			xStream.alias(o.getClass().getSimpleName(), o.getClass());
			xml = xStream.toXML(o);
		} catch (Exception e) {
			logger.error(e);
		}

		return xml;
	}
	
	
	public String getNodeValue(String nodePath)throws Exception{
		String returnvalue = "";
		try {
			returnvalue=(String)xpath.evaluate(nodePath, doc, XPathConstants.STRING);	
		} catch (Exception e) {
			logger.error(e);
		}
		
		return returnvalue;
	}
	
	
	public NodeList getNodeList(String nodeName){
		NodeList cusinfoList = null;
		try {
			cusinfoList=(NodeList)xpath.evaluate(nodeName, doc, XPathConstants.NODESET);
		} catch (Exception e) {
			logger.error(e);
		}
		return cusinfoList;
	}
	
	
	public String getNodeValueFromNode(String nodePath,Node node)throws Exception{
		try {
			System.out.println("node:"+node);
			return xpath.evaluate(nodePath, node);
		} catch (Exception e) {
			logger.error(e);
		}
		return "";
	}

}



使用例子

//初始化工具类,XmlStr是外部传入
		   XMLHelper helper = new XMLHelper(XmlStr);
		   
		   try {
			   //获取属性
			   System.out.println(helper.getNodeValue("/NodeCondition/nodeCode"));
			   
			   
			   //获取列表数据
			   NodeList nl = helper.getNodeList("/NodeCondition/contacts/cn.net.withub.smsp.orm.dc.ContactMan");
			   
			   //循环获取列表中详细属性
			   for(int j=0;j<nl.getLength();j++){
				     Node node=nl.item(j);
				     
				     String phoneNumber = helper.getNodeValueFromNode("phoneNumber", node);
				     System.out.println("phoneNumber:"+phoneNumber);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}



例子XML

<NodeCondition>
  <contacts>
    <cn.net.withub.smsp.orm.dc.ContactMan>
      <userName>a1</userName>
      <userType>1</userType>
      <phoneNumber>190000000</phoneNumber>
    </cn.net.withub.smsp.orm.dc.ContactMan>
    <cn.net.withub.smsp.orm.dc.ContactMan>
      <userName>a2</userName>
      <userType>3</userType>
      <phoneNumber>190000003</phoneNumber>
    </cn.net.withub.smsp.orm.dc.ContactMan>
  </contacts>
  <nodeCode>SP2001</nodeCode>
  <isSpecialSned>0</isSpecialSned>
</NodeCondition>

你可能感兴趣的:(java,xml)