免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 873 | 回复: 0
打印 上一主题 下一主题

java 读取xml [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-22 17:35 |只看该作者 |倒序浏览

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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP