Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3355850
  • 博文数量: 1450
  • 博客积分: 11163
  • 博客等级: 上将
  • 技术积分: 11101
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-25 14:40
文章分类

全部博文(1450)

文章存档

2017年(5)

2014年(2)

2013年(3)

2012年(35)

2011年(39)

2010年(88)

2009年(395)

2008年(382)

2007年(241)

2006年(246)

2005年(14)

分类: Java

2006-09-08 10:33:59

需要的包文件

//处理xml

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

//下面主要是org.xml.sax包的类
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;

//程序片断

try{
            
            //得到DOM解析器的工厂实例,得到javax.xml.parsers.DocumentBuilderFactory;类的实例就是我们要的解析器工厂
            DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
           
            //从DOM工厂获得DOM解析器,通过javax.xml.parsers.DocumentBuilderFactory实例的静态方法newDocumentBuilder()得到DOM解析器
            DocumentBuilder dombuilder=domfac.newDocumentBuilder();

            //解析xml生成xml文档
            Document doc = dombuilder.parse(httpconn.getInputStream());
           
            //得到根结点
            Element root=doc.getDocumentElement();
            //得到根结点的子结点
            NodeList nodes=root.getChildNodes();
            //System.out.println(nodes.getLength());

            if(nodes != null){
       
                for(int i=0;i                    node=nodes.item(i);
                    if(node.getNodeType()==Node.ELEMENT_NODE){
                        if(node.getNodeName()=="flag"){
                            flag = node.getFirstChild().getNodeValue();
                            continue;
                        }   
                        if(node.getNodeName()=="port"){
                            ports = node.getFirstChild().getNodeValue();
                            continue;
                        }
                        if(node.getNodeName()=="status"){
                            status = node.getFirstChild().getNodeValue();
                            continue;
                        }
                        if(node.getNodeName()=="diskspace"){
                            diskspace = node.getFirstChild().getNodeValue();
                            continue;
                        }
                    }
                }
}catch(Exception ex)
{
    ex.printStackTrace

}


阅读(942) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~