使用Java自带DOM工具解析XML

xml文件的格式为

<?xml version="1.0" encoding="GBK"?>
<CIBOFP>
	<HEAD>
		<APPCODE>111111</APPCODE>
		<BUSCD>1104</BUSCD>
		<EXSERIAL>75</EXSERIAL>
		<DATE>20150518</DATE>
		<TIME>183320</TIME>
	</HEAD>
	<BODY>
		<CREDITAPPID>75</CREDITAPPID>
		<MORTGAGORNO>借款人编号</MORTGAGORNO>
		<MORTGAGORNAME>借款人名名</MORTGAGORNAME>
		<STATUS>2</STATUS>
		<LIMITLIST>
			<LIMIT>
				<FINANCINGTYPE>1</FINANCINGTYPE>
				<AMOUNT>100000</AMOUNT>
			</LIMIT>
			<LIMIT>
				<FINANCINGTYPE>1</FINANCINGTYPE>
				<AMOUNT>200000</AMOUNT>
			</LIMIT>
			<LIMIT>
				<FINANCINGTYPE>1</FINANCINGTYPE>
				<AMOUNT>300000</AMOUNT>
			</LIMIT>
		</LIMITLIST>
		<REMARK>备注</REMARK>
	</BODY>
</CIBOFP>

 java的解析方法

import java.io.FileInputStream;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 测试使用java自带的xml解析器
 * @author test
 */
public class TestDomParse {

	public static void main(String[] args) {
		DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
		String filename = "E:\\workspaceeclipse\\common-xingye\\src\\main\\resources\\1004.xml";
		try {
			//产生解析实例
			DocumentBuilder dombuilder = domfac.newDocumentBuilder();
			//把XML文件读入放在FileInputStream中,并用其父类InputStream的实例接受
			InputStream is = new FileInputStream(filename);
			//根据输入XML文件流解析成Document结构,供下面遍历与操作使用
			Document doc = dombuilder.parse(is);
			//获取根结点CIBOFP
			Element root = doc.getDocumentElement();
			System.out.println(root.getTagName());
			//System.out.println(root.getTextContent());
			NodeList head= root.getElementsByTagName("HEAD");
			Node body= root.getElementsByTagName("BODY").item(0);
			
			//HEAD 的解析
			System.out.println(head.item(0).getNodeName()+"的解析开始");
			for(int i=0;i<head.getLength();i++){
				Node n = head.item(i);
				//System.out.println(n.getNodeName()+"    "+n.getTextContent());
				NodeList headNodeList = n.getChildNodes();
				for(int j=0;j<headNodeList.getLength();j++){
					Node hn = headNodeList.item(j);
					//使用hn.getNodeType()==Node.ELEMENT_NODE  防止打印空白
					if(hn.getNodeType()==Node.ELEMENT_NODE){
						System.out.println(hn.getNodeName()+"    "+hn.getTextContent());
					}
				}
				
			}
			
			//BODY的解析
			System.out.println(body.getNodeName()+"的解析开始");
			NodeList bodyList = body.getChildNodes();
			for(int a=0;a<bodyList.getLength();a++){
				Node n2 = bodyList.item(a);
				//防止打印空白
				//System.out.println(n.getNodeName()+" n type "+n.getNodeType());
				if(n2.getNodeType()==Node.ELEMENT_NODE){
					if(n2.getNodeName().equals("LIMITLIST")){
						System.out.println(n2.getNodeName()+"节点解析");
						//LIMITLIST 节点
						NodeList limitList = n2.getChildNodes(); 
						for(int b=0;b<limitList.getLength();b++){
							Node limit = limitList.item(b);
							//防止打印空白
							if(limit.getNodeType()==Node.ELEMENT_NODE){
								System.out.println(limit.getNodeName()+"节点解析");
								//LIMIT
								NodeList  t = limit.getChildNodes();
								for(int c=0;c<t.getLength();c++){
									Node l = t.item(c);
									//防止打印空白
									if(l.getNodeType()==Node.ELEMENT_NODE){
										System.out.println(l.getNodeName()+"    "+l.getTextContent());
									}
								}
								
							}
						}
					}else{
						System.out.println(n2.getNodeName()+"  "+n2.getTextContent());
					}
					
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}

 解析还是比较费劲

 

 

 

 

 

 

你可能感兴趣的:(解析xml)