Xml格式字符串的解析

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

   

1013

    batch_draw

    1900000107

    96e79218965eb72c92a549dd5a330112

    20140512131833895

    1900000107

    8nn0j1mfS

    10.191.82.165

    

        

            8ck7muvrD

            603324005200182020

            1066

            翁剑建

            500000

            1

            12

            574

            邮政储蓄银行总行(宁波)

            太保人寿

            

        

        

            8ck7muvpD

            6221883320023232895

            1066

            刘和开

            836000

            1

            12

            574

            邮政储蓄银行总行(宁波)

            太保人寿

            

        

    

    2

1336000


2、使用示例:

 

Element root =JDomUtils.string2Root(contentDecode, "GBK");
Element recordSet = root.getChild("record_set");
List 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(""+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包。

 

你可能感兴趣的:(Xml格式字符串的解析)