- 论坛徽章:
- 0
|
package com.wondertek.util;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.wondertek.bean.XMLBean;
public class GeneralXmlParser {
protected final Log log = LogFactory.getLog(getClass());
private static final String PARSESUCCESS = "000";//解析成功
private static final String PARSEFAIL = "001";//解析失败
private static final String CONNECTFAIL = "002";//网络连接失败
private List resultlist;
public GeneralXmlParser() {
this.resultlist = new ArrayList();;
}
/**
* @return List 返回所有查询记录, 每条记录以HASHMAP存储
*/
public List getResultList() {
return this.resultlist;
}
/**传入InputStream流进行XML解析
* @param is
* @return int值:0解析成功 1解析失败 2网络连接失败
*/
public String readXMLFile(String file){
if(file == null || Utility.null2Str(file).equals("")){
return CONNECTFAIL;
}else{
// 为解析XML作准备,创建DocumentBuilderFactory实例,指定DocumentBuilder
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException pce) {
log.debug(pce.toString());
return PARSEFAIL;
}
Document doc = null;
try {
doc = db.parse(file);
// getDomTree(doc);
} catch (SAXException se) {
log.debug(se.toString());
se.printStackTrace();
return PARSEFAIL;
} catch (IOException ioe) {
log.debug(ioe.toString());
return PARSEFAIL;
}
ArrayList list = new ArrayList();
// 下面是解析XML的全过程,只适用于...格式的xml
Element root = doc.getDocumentElement();
NodeList allnodes = root.getChildNodes();
for(int i=0;i0){
for(int j=0;j");
getDomTree(((Document)node).getDocumentElement());
break;
}
// print element and any attributes
case Node.ELEMENT_NODE:
{
System.out.print("");
if (node.hasChildNodes())
{
NodeList children = node.getChildNodes();
for (int i = 0; i ');
System.out.print("\t\n");
break;
}
// Print attribute nodes
case Node.ATTRIBUTE_NODE:
{
System.out.print(" " + node.getNodeName() + "=\"" +
((Attr)node).getValue() + "\"");
break;
}
// handle entity reference nodes
case Node.ENTITY_REFERENCE_NODE:
{
System.out.print("&");
System.out.print(node.getNodeName());
System.out.print(";");
break;
}
// print cdata sections
case Node.CDATA_SECTION_NODE:
{
// System.out.print("");
nodevalue = node.getNodeValue();
// System.out.println(nodevalue);
break;
}
// print text
case Node.TEXT_NODE:
{
System.out.print(node.getNodeValue());
break;
}
case Node.COMMENT_NODE:
{
System.out.print("");
break;
}
// print processing instruction
case Node.PROCESSING_INSTRUCTION_NODE:
{
System.out.print("");
break;
}
}
return node.getNodeValue();
} // printDomTree(Node)
public static void main(String[] args) {
GeneralXmlParser parser= new GeneralXmlParser();
try {
String file = "D:/Morgan/20060921sample.xml";
String i =parser.readXMLFile(file);
System.out.println(i);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("size is "+parser.getResultList().size());
XMLBean bean;
for (int i = 0; i
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/15895/showart_175237.html |
|