package mfutil;
import java.io.*;
import java.util.*;
import org.jdom.*;//XML
import org.jdom.input.*;
import paytv.*;/**
 * Use to get the xml node value,but not get the attribute value.
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */
public class GetXMLSetting
{
    private String xmlpath;
    private SAXBuilder saxb;
    private Document doc;    /**
     * Initalize the xml file
     * @param xmlpath the location where the xml locates
     * @throws FileNotFoundException if the xml file doesn't exist,throw it.
     */
    public GetXMLSetting(String xmlpath)
    {
        try
        {
            File fl = new File(xmlpath);
            if(!fl.exists())
            {
                System.out.println("Not Find xml file:" + xmlpath);
                throw new java.io.FileNotFoundException("Not Find xml file:" + xmlpath);
            }
            this.xmlpath = xmlpath;
            saxb = new SAXBuilder();
            doc = saxb.build(this.xmlpath);
        }catch(org.jdom.JDOMException e)
        {
            throw new mfutil.MFUtilException("Can not initalize the xml file,pls check the xml format. Path:" + xmlpath);
        }
        catch(java.io.FileNotFoundException e)
        {
            throw new mfutil.MFUtilException("Not Find xml file:" + xmlpath);
        }
    }    /**
     * Get the node value by specified rootname and child name
     * E.g
     * <ENVIRONMENT>
     *    <KASENNA>
     *       <MPEGPATH>
     *                 myvalue
     *       </MPEGPATH>
     *    </KASENNA>
     * </ENVIRONMENT>
     * getPropertise("KASENNA","MPEGPATH") would return 'myvalue'
     * Note that the '<ENVIRONMENT>' would be ignore
     * @param rootname
     * @param childname
     * @return
     */
    public String getPropertise(String rootname,String childname)
    {
        String str = "";
        try
        {
            Element el = doc.getRootElement();
            Element ka = el.getChild(rootname);
            Element pl = ka.getChild(childname);
            str = pl.getText();
        }
        catch(Exception e)
        {
            System.out.println("Can not get the xml setting parameter:" + rootname + " " + childname);
            System.out.println(e.getMessage());
            throw new mfutil.MFUtilException("Can not get the xml setting parameter:" + rootname + " " + childname);
        }
        finally
        {
            return str.trim();
        }
    }    public String getPropertise(String rootname,String childname,String subchildname)
    {
        String str = "";
        try
        {            Element el = doc.getRootElement();
            Element ka = el.getChild(rootname);
            str = ka.getChild(childname).getChildText(subchildname);
        }
        catch(Exception e)
        {
            System.out.println("Can not get the xml setting parameter:" + rootname + " " + childname);
            System.out.println(e.getMessage());
        }
        finally
        {
            return str.trim();
        }
    }    public String getPropertise(String rootname,String childname,String subchildname,String subchildname2)
    {
        String str = "";
        try
        {            Element el = doc.getRootElement();
            Element ka = el.getChild(rootname);
            str = ka.getChild(childname).getChild(subchildname).getChildText(subchildname2);
        }
        catch(Exception e)
        {
            System.out.println("Can not get the xml setting parameter:" + rootname + " " + childname);
            System.out.println(e.getMessage());
        }
        finally
        {
            return str.trim();
        }
    }
    /**
     * Get the attribute value by specified root name,child name,and childvalue,attriubte name
     *
     * E.g:
     *  <WEBCHAN>
                <CHANNUM url="/paytv/common/epg/channel/ch61/61.html" chi_channel_name="KTV">
                        61
                </CHANNUM>
                <CHANNUM url="/paytv/common/epg/channel/ch65/65.html" chi_channel_name="CCTV">
                        65
     *  </CHANNUM>
     *
     * String str = gxs.getAttribute("WEBCHAN","CHANUM","61","url");
     * return /paytv/common/epg/channel/ch65/61.html
     *
     * @param rootname the root element
     * @param childname child element
     * @param childvalue child value
     * @param attname attribute name     * @return attribute value
     */
    public String getAttribute(String rootname,String childname,String childvalue,String attname)
    {
        String str = "";
        try
        {
            Element el = doc.getRootElement();
            Element ka = el.getChild(rootname);
            List lchild = ka.getChildren();
            //System.out.println(lchild.size());
            for(int i=0;i<lchild.size();i++)
            {
                Element ka2 = (Element)lchild.get(i);
                if(ka2.getTextTrim().equalsIgnoreCase(childvalue.trim()))
                {
                    //                    System.out.println("find it");
                    str = ka2.getAttributeValue(attname);
                }
            }
        }
        catch(Exception e)
        {
            System.out.println("Can not get the xml setting parameter:" + rootname + " " + childname + " " +
                               childvalue + " " + attname);
            System.out.println(e.getMessage());
            throw new mfutil.MFUtilException("Can not get the xml setting parameter:" + rootname + " " + childname + " " +
                               childvalue + " " + attname);
        }
        finally
        {
            return str.trim();
        }
    }
    /**
     * Get the children number of the specified parent
     *
     *
     * E.g:
     *  <WEBCHAN>
                <CHANNUM url="/paytv/common/epg/channel/ch61/61.html" chi_channel_name="KTV">
                        61
                </CHANNUM>
                <CHANNUM url="/paytv/common/epg/channel/ch65/65.html" chi_channel_name="CCTV">
                        65
     *  </CHANNUM>
     *
     *   System.out.println(gxs.getChildrenNum("WEBCHAN","CHANUM"));
     *   return 2
     *
     * @param rootname parent element
     * @param childname the children name
     *
     * @return the children number
     */
    public int getChildrenNum(String rootname,String childname)
    {
        int iReturn = 0;
        String str = "";
         try
         {
             Element el = doc.getRootElement();
             Element ka = el.getChild(rootname);
             iReturn = ka.getChildren(childname).size();
         }
         catch(Exception e)
         {
             System.out.println("Can not get the xml setting parameter:" + rootname + " " + childname);
             System.out.println(e.getMessage());
             throw new mfutil.MFUtilException("Can not get the xml setting parameter:" + rootname + " " + childname);
         }
         finally
         {
             return iReturn;
        }
    }

解决方案 »

  1.   

    public ArrayList getChildValues(String rootname,String childname,String subchildname)
        {
            int iReturn = 0;
            ArrayList al = new ArrayList();
            String str = "";
             try
             {
                 Element el = doc.getRootElement();
                 Element ka = el.getChild(rootname);
                 Element chi = ka.getChild(childname);
                 //System.out.println(chi.getChildren(subchildname).size());
                 for(int i=0;i<chi.getChildren(subchildname).size();i++)
                 {
                     al.add( ((Element)chi.getChildren(subchildname).get(i)).getTextTrim() );
                 }         }
             catch(Exception e)
             {
                 System.out.println("Can not get the xml setting parameter:" + rootname + " " + childname);
                 System.out.println(e.getMessage());
                 throw new mfutil.MFUtilException("Can not get the xml setting parameter:" + rootname + " " + childname);
             }
             finally
             {
                 return al;
            }
        }    public static void test()
        {
            GetXMLSetting gxs = new GetXMLSetting(paytv.paytvutil.getEPGSettingPath() + java.io.File.separator + "epgsetting.xml");
            String str = gxs.getAttribute("WEBCHAN","CHANUM","61","url");
            System.out.println(str);
            System.out.println(gxs.getChildrenNum("WEBCHAN","CHANUM"));
            ArrayList al = gxs.getChildValues("EPG","VALIDATE_SCHEDULE","CHANNEL_NUM");
            for(int i=0;i<al.size();i++)
            {
                System.out.println((String)al.get(i));
            }
        }
        public static void main(String arg[]) throws java.io.FileNotFoundException
        {
            GetXMLSetting.test();
            //System.out.println(paytv.paytvutil.getEPGSettingPath() + java.io.File.separator + "epgsetting.xml");
            //System.out.println(str);
        }}
      

  2.   

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              DocumentBuilder db = dbf.newDocumentBuilder();          org.xml.sax.InputSource is = new org.xml.sax.InputSource();
              is.setCharacterStream(new StringReader(purview));//pruview is xml Stirng          Document doc = db.parse(is);
              NodeList nlPrivileges = doc.getDocumentElement().getElementsByTagName("category");
              for (int i = 0; i < nlPrivileges.getLength(); i++) {
                String path = ( (Element) nlPrivileges.item(i)).getAttribute("id");
                String enableValue = ( (Element) nlPrivileges.item(i)).getFirstChild().
                    getNodeValue();
                         }
      

  3.   

    这个看看:
    <%@ page import="javax.xml.parsers.*"%>
    <%@ page import="org.w3c.dom.*"%>
    <%@ page import="java.io.*"%>    //获得一个xml解析器
        DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
        DocumentBuilder builder=factory.newDocumentBuilder();
        //解析XML文件
        Document document= builder.parse(new File("config.xml"));
        document.normalize();    //获取所有的"Path"标记,它是一个NodeList
        NodeList ListX=document.getElementsByTagName("Path");
        for(int i=0;i<ListX.getLength();i++){
          //获取Configuration的每一个元素
          Element Configurations=(Element)ListX.item(i);
          //取Jsp物理路径
          //JspPath=Configurations.getElementsByTagName("JSPPath").item(0).getFirstChild().getNodeValue().toString();
          //取Asp物理路径
          AspPath=Configurations.getElementsByTagName("ASPPath").item(0).getFirstChild().getNodeValue().toString();
        }