Java DOM 实现方法

package com.b2cquery.icbc;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DOM {
	
	public Vector<Vector<String>> ICBCworkingDOM() {
		DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
		Vector<Vector<String>> queryresult = new Vector<Vector<String>>();
		Vector<String> rowdata = new Vector<String>();
		Double totalmoney = 0.00;
		boolean tag = true;
		String nexttag ="";
		try {
			int rdcount = 0;
			DocumentBuilder dombuilder = domfac.newDocumentBuilder();
			//ICBCB2CQuery icbc = new ICBCB2CQuery();

			//InputStream is = icbc.B2CQueryByPage("20110407", "20110407", "");
			InputStream is = new FileInputStream("E:/icbc.xml");
			Document doc = dombuilder.parse(is);
			Element CMS = doc.getDocumentElement();
			NodeList eb = CMS.getChildNodes();
			for (int i = 0; i < eb.getLength(); i++) {
				// get eb
				if (eb.item(i).getNodeType() == Node.ELEMENT_NODE && eb.item(i).getNodeName().equals("eb")) {
					//System.out.println(eb.item(i).getNodeName());
					//System.out.println("I'm eb!");
					NodeList eblist = eb.item(i).getChildNodes();
					for (int j = 0; j < eblist.getLength(); j++) {
						//get pub
//						if (eblist.item(j).getNodeType() == Node.ELEMENT_NODE && eblist.item(j).getNodeName().equals("pub")){
//							NodeList pubList = eblist.item(j).getChildNodes();
//							for (int k = 0; k < pubList.getLength(); k++) {
//								if(pubList.item(k).getNodeType() == Node.ELEMENT_NODE){
//									System.out.println(pubList.item(k).getNodeName());
//								}
//							}
//						}
						//get out
						if (eblist.item(j).getNodeType() == Node.ELEMENT_NODE && eblist.item(j).getNodeName().equals("out")) {
							NodeList outList = eblist.item(j).getChildNodes();
							//System.out.println(eblist.item(j).getNodeName());

							for (int k = 0; k < outList.getLength(); k++) {
								//get NextTag
								if (outList.item(k).getNodeType() == Node.ELEMENT_NODE&& outList.item(k).getNodeName().equals("NextTag")){
									String getNextTag = outList.item(k).getTextContent();
									if (getNextTag.equals("") == false) {
										tag = true;
										nexttag = getNextTag;
									}
									else{
										tag = false;
										nexttag = "";
									}
								}
								//getrd
								if (outList.item(k).getNodeType() == Node.ELEMENT_NODE&& outList.item(k).getNodeName().equals("rd")) {
									rdcount++;
									//System.out.println(outList.item(k).getNodeName());
									//System.out.println("I'm rd!");
									NodeList rdList = outList.item(k).getChildNodes();
									//get orderInfo
									for (int l = 0; l < rdList.getLength(); l++) {
										if (rdList.item(l).getNodeType() == Node.ELEMENT_NODE && rdList.item(l).getNodeName().equals("SerialNo")) {
											rowdata.add(rdList.item(l).getTextContent());

										}
										if (rdList.item(l).getNodeType() == Node.ELEMENT_NODE && rdList.item(l).getNodeName().equals("OrderNum")) {
											rowdata.add(rdList.item(l).getTextContent());
											
										}
										if (rdList.item(l).getNodeType() == Node.ELEMENT_NODE && rdList.item(l).getNodeName().equals("OrderAmt")) {
											String amt = rdList.item(l).getTextContent();
											int amtL = amt.length();
											String  temphead = amt.substring(0, amtL-2);
											String  temptail = amt.substring(amtL-2,amtL);
											String  finallyhamt = temphead + "." + temptail;
											totalmoney = totalmoney + Double.valueOf(finallyhamt);
											rowdata.add(finallyhamt);
											
										}
										if (rdList.item(l).getNodeType() == Node.ELEMENT_NODE && rdList.item(l).getNodeName().equals("TranTime")) {
											rowdata.add(rdList.item(l).getTextContent());
											
										}
										if (rdList.item(l).getNodeType() == Node.ELEMENT_NODE && rdList.item(l).getNodeName().equals("Result")) {
											rowdata.add(rdList.item(l).getTextContent());
											queryresult.add(rowdata);
											rowdata = new Vector<String>();
										}
										
									}
								}
							}
						}

					}
				}
			}
			return queryresult;

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			return null;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (SAXException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	public DOM(){
		
	}
    

    public static void main(String[] args) 
    {
    	Vector<Vector<String>> queryresult = new DOM().ICBCworkingDOM();
    	int i = 0; 
    	for (Vector<String> vector : queryresult) {
    		i++;
			for (String string : vector) {
				//System.out.println(string);
			}
		}
    	loger.info(""+i);
    }
    private static final Logger loger = Logger.getLogger("DOM");
}

你可能感兴趣的:(java,cms,xml,J#)