以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XML工具及XML开发环境 』  (http://bbs.xml.org.cn/list.asp?boardid=7)
----  求助 急需xml解析器的源代码 谢谢~~~  (http://bbs.xml.org.cn/dispbbs.asp?boardid=7&rootid=&id=46959)


--  作者:小求951
--  发布时间:5/16/2007 12:27:00 AM

--  求助 急需xml解析器的源代码 谢谢~~~
同标题
--  作者:tiddy
--  发布时间:6/6/2007 4:27:00 PM

--  
没有啊
--  作者:tiddy
--  发布时间:6/6/2007 4:28:00 PM

--  
顶顶顶啊
--  作者:DUT_girl
--  发布时间:7/3/2007 12:49:00 PM

--  
I do
--  作者:Bronze83
--  发布时间:7/18/2007 11:50:00 AM

--  
import org.jdom.*;
import org.jdom.input.*;
import java.io.*;
import java.util.*;

/**
* <p>Title: parser</p>
*
* <p>Description: To parser a XML document</p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: jclt</p>
*
* @author 天不流云
* @version 1.0
*/
public class XmlReader {
    private Element m_RootElement = null;
    /*
     *构造函数
     *根据文件的路径初始化dom的根
     */
    public XmlReader(String xmlFile) {
        SAXBuilder builder = new SAXBuilder(); //定义解析器
        Document doc = null;
        try {
            doc = builder.build(new FileInputStream(xmlFile)); //读入XML文件,获得doc
            this.m_RootElement = doc.getRootElement(); //获得XML最上面的根
        } catch (IOException ex) {
            ex.printStackTrace();
            this.m_RootElement = null;
        } catch (JDOMException ex) {
            System.out.print(ex.getMessage());
            this.m_RootElement = null;
        }
    }

    /*
     *
     *获得指定名字的根的内容
     */
    public List getElement(Element curRoot, String codeName) {
        List result = null;
        if (curRoot == null) {
            curRoot = m_RootElement;
        } //判断xml文件是否存在,并确定是否被解析过

        if (curRoot != null) {
            List l = curRoot.getChildren(); //获得最上层根的子节点
            Iterator it = l.iterator(); //递归取出
            while (it.hasNext()) {
                Element e = (Element) it.next();
                if (e.getAttributeValue("name").toString().equals(codeName)) { //获取这些根是否是所需要的
                    List l1 = e.getChildren(); //如果需要解析出这个节点的所有子节点
                    Iterator it1 = l1.iterator();
                    while (it1.hasNext()) {
                        Element e1 = (Element) it1.next();
                        result.add(e1.getTextTrim()); // 取出结果,将结果放到结果集中
                    }
                    break;
                }
            }
        }
        return result;
    }

    /*
     *
     *检查值codename的第二级子元素的name值是否存在(即:<group name="">)
     */

    public Element checkElement(Element curRoot, String codeName) {
        Element element = null;
        if (curRoot == null) {
            curRoot = this.m_RootElement;
        }
        if (curRoot != null) {
            List l = curRoot.getChildren(); //获得最上层的子节点
            Iterator it = l.iterator(); //利用迭代器递归取出
            while (it.hasNext()) {
                Element e = (Element) it.next();
                if (e.getAttributeValue("name").toString().equals(codeName)) {
                    element = e;
                    break;
                }
            }
        }
        return element;
    }
    /*
    *检查值codename值的第三级目录是否存在(<key,key>)
    *
    */
   public Element checkElementc(Element curRoot,String codeNamep,String codeNamec){
       Element element = null;
       if(curRoot == null){
           curRoot = this.m_RootElement;
       }
       if(curRoot!= null){
           List l =curRoot.getChildren(); // 获得最上层的所有子节点
           Iterator it = l.iterator();
           while(it.hasNext()){
               Element e = (Element)it.next();
               if(e.getAttributeValue("name").toString().equals(codeNamep)){
                   List l1=e.getChildren();
                   Iterator it1 = l1.iterator();
                   while(it1.hasNext()){
                       Element e1=(Element)it1.next();
                       if(e1.getTextTrim().equals(codeNamec)){ //比较第三级子元素
                           element = e1;
                           break;
                       }
                   }
                   break;
               }
           }
       }
       return element;
   }

   public Element getM_RootElement(){
       return m_RootElement;
   }

   public void setM_RootElement(Element m_RootElement){
       this.m_RootElement=m_RootElement;
   }

}


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
62.500ms