Java XML 简要读写工具

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

[color=red]工具类[/color]

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 "";
}

}



[color=red]使用例子[/color]


//初始化工具类,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();
}



[color=red]例子XML[/color]


<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值