JAva dom解析xml例

4485 ワード

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をメモリにロードし、解析します.