java dom 解析xml例子

XML格式如下:

<?xml version="1.0" encoding="GBK"?>
<elXmlIniPlus>
	<Section SectionName="DCQRYITFA">
		<elXmlProperty>
			<Attribute key="PRJCOD" value="NJ012001" ></Attribute>
			<Attribute key="BNKTYP" value="CMBHZ" ></Attribute>
			<Attribute key="BNKCOD" value="CMB" ></Attribute>
			<Attribute key="VERSON" value="A" />
			<Attribute key="ITFNAM" value="招行杭州" />
			<Attribute key="TRSFLG" value="Y" />
			<Attribute key="BGNPAY" value="08:00:00" />
			<Attribute key="ENDPAY" value="20:00:00" />
			<Attribute key="INSFLG" value="N" />
			<Attribute key="BGNTIM" value="04:00:00" />
			<Attribute key="ENDTIM" value="23:00:00" />
		</elXmlProperty>
	</Section>
	<Section SectionName="SYCOMRETZ">
		<elXmlProperty>
			<Attribute key="ERRCOD" value="0000000" />
			<Attribute key="ERRMSG" value="" />
		</elXmlProperty>
	</Section>
	<Section SectionName="DCQRYITFQ">
		<elXmlProperty>
			<Attribute key="PRJCOD" value="NJ012004" ></Attribute>
			<Attribute key="BNKTYP" value="CMBHZW" ></Attribute>
			<Attribute key="BNKCOD" value="CMB" ></Attribute>
			<Attribute key="VERSON" value="A" />
			<Attribute key="ITFNAM" value="招行杭州WW" />
			<Attribute key="TRSFLG" value="Y" />
			<Attribute key="BGNPAY" value="08:00:00" />
			<Attribute key="ENDPAY" value="20:00:00" />
			<Attribute key="INSFLG" value="N" />
			<Attribute key="BGNTIM" value="04:00:00" />
			<Attribute key="ENDTIM" value="23:00:00" />
		</elXmlProperty>
	</Section>
</elXmlIniPlus>

 

 

 

处理类:

package com.cmb.fbcbcserver.api.internal.parser.xml;

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;

public class IniPlus {

	/**
	 * <Description>
	 *
	 * @since Aug 2, 2012
	 * @param args <Description>
	 * @throws Exception 
	 *
	 */
	public static void main(String[] args) throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder
				.parse("E:\\2012Project\\CbcSdk1.0\\src\\com\\cmb\\fbcbcserver\\api\\internal\\parser\\xml\\aa.xml");

		Element root = doc.getDocumentElement();
		System.out.println("根节点名字:" + root.getTagName());

		NodeList secNodes = root.getElementsByTagName("Section");
		for (int i = 0; i < secNodes.getLength(); i++) {
			Element secElement = (Element) secNodes.item(i);
			System.out.println("section name:"+secElement.getAttribute("SectionName"));
			NodeList proNodes = secElement.getElementsByTagName("elXmlProperty");
			for (int j = 0; j < proNodes.getLength(); j++) {
				Element proElement = (Element) proNodes.item(j);
				NodeList attriNodes = proElement.getChildNodes();
				for (int z = 0; z < attriNodes.getLength(); z++) {
					if (attriNodes.item(z).getNodeType() == Node.ELEMENT_NODE) {
						Element attriElement = (Element)attriNodes.item(z);
						System.out.println("key:"+attriElement.getAttribute("key")+" value:" + attriElement.getAttribute("value"));
					}
				}
			}
			System.out.println("--------");
		}
	}
}

 

 请注意:dom会将所有xml加载到内存中,然后解析。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值