Bootstrap

Xml格式字符串的解析

1、解析比如这种格式的XML格式的字符串:

   

<op_code>1013</op_code>

    <op_name>batch_draw</op_name>

    <op_user>1900000107</op_user>

    <op_passwd>96e79218965eb72c92a549dd5a330112</op_passwd>

    <op_time>20140512131833895</op_time>

    <sp_id>1900000107</sp_id>

    <package_id>8nn0j1mfS</package_id>

    <client_ip>10.191.82.165</client_ip>

    <record_set>

        <record>

            <serial>8ck7muvrD</serial>

            <rec_bankacc>603324005200182020</rec_bankacc>

            <bank_type>1066</bank_type>

            <rec_name>翁剑建</rec_name>

            <pay_amt>500000</pay_amt>

            <acc_type>1</acc_type>

            <area>12</area>

            <city>574</city>

            <subbank_name>邮政储蓄银行总行(宁波)</subbank_name>

            <desc>太保人寿</desc>

            <recv_mobile></recv_mobile>

        </record>

        <record>

            <serial>8ck7muvpD</serial>

            <rec_bankacc>6221883320023232895</rec_bankacc>

            <bank_type>1066</bank_type>

            <rec_name>刘和开</rec_name>

            <pay_amt>836000</pay_amt>

            <acc_type>1</acc_type>

            <area>12</area>

            <city>574</city>

            <subbank_name>邮政储蓄银行总行(宁波)</subbank_name>

            <desc>太保人寿</desc>

            <recv_mobile></recv_mobile>

        </record>

    </record_set>

    <total_num>2</total_num>

<total_amt>1336000</total_amt>


2、使用示例:

 

Element root =JDomUtils.string2Root(contentDecode, "GBK");
Element recordSet = root.getChild("record_set");
List<Element> records= recordSet.getChildren("record");
String recordNum=String.valueOf(records.size());//和totalNum比较
String totalAmt = root.getChildText("total_amt");  //总金额

for (Element element : records) {
String serial=element.getChildText("serial");//单笔序列号
String recBankAcc = element.getChildText("rec_bankacc");//收款方银行帐号
String bankType =element.getChildText("bank_type");//银行类型
}


 

3、主要核心方法:

     

import java.io.ByteArrayInputStream;
import java.io.UTFDataFormatException;
import java.io.UnsupportedEncodingException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import com.cpic.ebg.common.util.exception.BankServiceException;
import com.cpic.ebg.common.util.exception.BankServiceExceptionConverter;

public class JDomUtils {
	//创建根节点
public static Element createRoot(String rootName){
		Element root = new Element(rootName);
		return root;
}
	//增加孩子节点
public static Element addChild(Element parent, String childName){
		Element child = new Element(childName);
		parent.addContent(child);
		return child;
	}
	//增加有值的孩子节点
public static Element addChild(Element parent, String childName, String childValue){
		Element child = new Element(childName);
		child.setText(childValue == null ? "" : childValue);
		parent.addContent(child);
		return child;
	}
	//将字符串转化为根节点
public static Element string2Root(String content,String encoding) throws BankServiceException{
	Document document = string2Document(content,encoding);
		return document.getRootElement();
	}
	//将String转化为document对象
private static Document string2Document(String content,String encoding) throws BankServiceException {
		int startPoint = content.indexOf("<?xml");
		if(-1 != startPoint){
			content = content.substring(startPoint);
		}
		if(-1 == content.indexOf("<?xml")){
			content = "<?xml version=\"1.0\" encoding = \""+encoding+"\"?>"+content;
		}
		
	try {
		ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes(encoding));
			SAXBuilder builder = new SAXBuilder();
			return builder.build(bais);
		} catch (UnsupportedEncodingException e) {
			throw BankServiceExceptionConverter.convert(e, "不支持字符集UTF-8");
		} catch(UTFDataFormatException e){
			throw BankServiceExceptionConverter.convert(e, "字符集不符,实际报文字符集不是"+encoding);
		}catch (Exception e) {
			throw BankServiceExceptionConverter.convert(e, "报文不是xml格式,其内容为 :"+content);
		} 
	}
}


 

 

4、使用的是jdom-1.0.jar包。

 

;