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();
        }
    }

解决方案 »

  1.   


        /**
         * 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;
            }
        }    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("KASENNA","VALIDATE","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.   

    用jdom吧 :)
    http://www.csdn.net/Develop/Read_Article.asp?Id=20720