我传了个例子,和我做的解析xml文件的jsp程序。
麻烦各位了,小女子谢过了。
比如说下面的例子:
这个是原来的XML文件 
<?xml version="1.0" encoding="UTF-8" ?> 
- <repository name="compass-solutions" visible="true" displaytype="icons">
  <title>Compass商业智能平台</title> 
  <description>通过创建统一的数据平台,满足自动报表编制需求,为领导经营决策提供辅助支持。</description> 
  <icon>getImage?image=icons/folder.png</icon> 
  <rollovericon>getImage?image=icons/solutions.png</rollovericon> 
  <solution /> 
- <file type="FILE.URL" visible="true" displaytype="url">
  <filename>Portal.url</filename> 
  <title>Portal演示</title> 
  <target>pentaho_portal</target> 
  <description>Pentaho Portal演示程序包括了所有可获得的例子和一些附加的仪表盘例子。</description> 
  <icon>getImage?image=icons/url.png</icon> 
  <url>/portal/portal/pentaho/default</url> 
  </file>
- <file type="FILE.FOLDER" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" name="samples" path="" visible="true" displaytype="list">
  <title>应用程序</title> 
  <description>一些例子,用来展示Compass BI平台的特性。</description> 
  <icon>reporting.png</icon> 
  <solution>samples</solution> 
- <file type="FILE.ACTIVITY" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" visible="true" displaytype="report">
  <filename>ldcx.xaction</filename> 
  <path /> 
  <solution>samples</solution> 
  <title>ldcx</title> 
  <description>ldcx</description> 
  <author>Cube Designer</author> 
  <icon>slicedice.png</icon> 
  <properties>subscribable=false</properties> 
  </file>
- <file type="FILE.ACTIVITY" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" visible="true" displaytype="report">
  <filename>u.xaction</filename> 
  <path /> 
  <solution>samples</solution> 
  <title>u</title> 
  <description>u</description> 
  <author>Cube Designer</author> 
  <icon>slicedice.png</icon> 
  <properties>subscribable=false</properties> 
  </file>
- <file type="FILE.FOLDER" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" name="bursting" path="bursting" visible="true" displaytype="list">
  <path>bursting</path> 
  <title>5. Bursting例子</title> 
  <description>Bursting允许你把一个大任务(例如把报表传输给部门经理)分解成一个简单的、对于每个单独案例都可重复使用的流程。 <br/>The rules that determine the cases, and the processes for creating the content and delivering it can be customized to meet the requirements.</description> 
  <icon>getImage?image=icons/burst.png</icon> 
  <solution>samples</solution> 
  </file>
- <file type="FILE.FOLDER" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" name="charts" path="charts" visible="true" displaytype="list">
  <path>charts</path> 
  <title>8. 图表例子</title> 
  <description>使用多种数据来显示不同的图表例子</description> 
  <icon>getImage?image=icons/Charts.png</icon> 
  <solution>samples</solution> 
  </file>
- <file type="FILE.FOLDER" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" visible="false" name="chinaUnionPay">
  <path>chinaUnionPay</path> 
  <title>chinaUnionPay</title> 
  </file>
- <file type="FILE.FOLDER" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" name="dashboard" path="dashboard" visible="true" displaytype="list">
  <path>dashboard</path> 
  <title>仪表盘</title> 
  <description /> 
  <icon>getImage?image=icons/dashboard.jpg</icon> 
  <solution>samples</solution> 
  </file>
- <file type="FILE.FOLDER" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" name="datasources" path="datasources" visible="false" displaytype="list">
  <path>datasources</path> 
  <title>7. 数据源例子</title> 
  <description>从不同OLAP和关系型数据库数据源返回的数据集例子</description> 
  <icon>getImage?image=icons/datasources.png</icon> 
  <solution>samples</solution> 
  </file>
- <file type="FILE.FOLDER" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" name="getting-started" path="getting-started" visible="true" displaytype="list">
  <path>getting-started</path> 
  <title>1. 入门</title> 
  <description>一些例子,用来检查你的Compass BI平台是否安装正确.</description> 
  <icon>getImage?image=icons/GettingStarted.png</icon> 
  <solution>samples</solution> 
  </file>
- <file type="FILE.FOLDER" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" visible="false" name="index">
  <path>index</path> 
  <title>index</title> 
  </file>
- <file type="FILE.FOLDER" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" visible="false" name="portal">
  <path>portal</path> 
  <title>portal</title> 
  </file>
- <file type="FILE.FOLDER" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" name="printing" path="printing" visible="false" displaytype="list">
  <path>printing</path> 
  <title>4. 打印例子</title> 
  <description>内容发送到打印机的例子。 Content -> Printer. 尝试一下?</description> 
  <icon>getImage?image=icons/printing.png</icon> 
  <solution>samples</solution> 
  </file>
- <file type="FILE.FOLDER" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" name="reporting" path="reporting" visible="true" displaytype="list">
  <path>reporting</path> 
  <title>2. 报表例子</title> 
  <description>使用Eclipse BIRT和 JasperReports作为报表引擎并生成HTML和PDF报表的例子。</description> 
  <icon>getImage?image=icons/reporting.png</icon> 
  <solution>samples</solution> 
  </file>
- <file type="FILE.FOLDER" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" name="services" path="services" visible="false" displaytype="list">
  <path>services</path> 
  <title>Web Service例子</title> 
  <description>这个例子运行一个web service,并返回一个SOAP相应</description> 
  <icon>getImage?image=icons/WebServiceExample.png</icon> 
  <solution>samples</solution> 
  </file>
- <file type="FILE.FOLDER" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" name="svg" path="svg" visible="false" displaytype="list">
  <path>svg</path> 
  <title>SVG Examples</title> 
  <description>Examples of using SVG to display data graphically</description> 
  <icon>getImage?image=icons/svg.png</icon> 
  <solution>samples</solution> 
  </file>
- <file type="FILE.FOLDER" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" visible="false" name="Widgets">
  <path>Widgets</path> 
  <title>Widgets</title> 
  </file>
  </file>
- <file type="FILE.FOLDER" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" name="test" path="" visible="true" displaytype="list">
  <title>测试目录</title> 
  <description /> 
  <icon>getImage?image=icons/reporting.png</icon> 
  <solution>test</solution> 
- <file type="FILE.FOLDER" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" name="cube" path="cube" visible="true" displaytype="list">
  <path>cube</path> 
  <title>多维</title> 
  <description /> 
  <icon>getImage?image=icons/reporting.png</icon> 
  <solution>test</solution> 
  </file>
- <file type="FILE.FOLDER" aclAdministration="true" aclExecute="true" aclSubscribe="true" aclModifyAcl="true" name="report" path="report" visible="true" displaytype="list">
  <path>report</path> 
  <title>报表</title> 
  <description /> 
  <icon>getImage?image=icons/reporting.png</icon> 
  <solution>test</solution> 
  </file>
  </file>
  </repository>

解决方案 »

  1.   

    这个是解析后的文件<?xml version="1.0" encoding="GB2312" ?> 
    - <xmlmenu arrow="images/arrow.right.gif" text-overcolor="white" style="horizontal">
      <menu id="2" url="/portal/portal/pentaho/default" icon="getImage?image=icons/url.png" title="" text="Portal演示" /> 
    - <menu id="3" url="" icon="reporting.png" title="" text="应用程序">
      <menu id="31" url="" icon="slicedice.png" title="ldcx" text="ldcx" /> 
      <menu id="32" url="" icon="slicedice.png" title="u" text="u" /> 
      <menu id="33" url="" icon="getImage?image=icons/burst.png" title="" text="5.Bursting例子" /> 
      <menu id="34" url="" icon="getImage?image=icons/Charts.png" title="" text="8.图表例子" /> 
      <menu id="35" url="" icon="getImage?image=icons/dashboard.jpg" title="" text="仪表盘" /> 
      <menu id="36" url="" icon="getImage?image=icons/GettingStarted.png" title="" text="1.入门" /> 
      <menu id="37" url="" icon="getImage?image=icons/reporting.png" title="" text="2.报表例子" /> 
      </menu>
    - <menu id="4" url="" icon="getImage?image=icons/reporting.png" title="" text="测试目录">
      <menu id="41" url="" icon="getImage?image=icons/reporting.png" title="" text="多维" /> 
      <menu id="41" url="" icon="getImage?image=icons/reporting.png" title="" text="报表" /> 
      </menu>
      </xmlmenu>
      

  2.   

    这个是解析文件,只能解析到第二层,第三层后的元素都不能解析,麻烦各位大哥大姐帮帮忙。
    <%@page
    language="java"
    pageEncoding="GBK"%>
    <%@ page import="java.io.*,
        java.util.*,
    javax.servlet.ServletException,
    javax.servlet.http.HttpServletRequest,
    javax.servlet.http.HttpServletResponse,
    javax.servlet.http.HttpSession,
    javax.xml.parsers.DocumentBuilder,
    javax.xml.parsers.DocumentBuilderFactory,
    javax.xml.parsers.ParserConfigurationException,org.apache.commons.logging.Log,
    org.apache.commons.logging.LogFactory,org.dom4j.Document,
    org.dom4j.DocumentException,
    org.dom4j.DocumentHelper,
    org.dom4j.Element,
    org.pentaho.core.session.IPentahoSession,
    org.pentaho.core.solution.HttpRequestParameterProvider,
    org.pentaho.core.solution.HttpSessionParameterProvider,
    org.pentaho.core.system.PentahoSystem,
    org.pentaho.core.ui.SimpleUrlFactory,
    org.pentaho.core.util.UIUtil,
    org.pentaho.messages.util.LocaleHelper,
    org.pentaho.ui.component.INavigationComponent,
    org.pentaho.ui.component.NavigationComponentFactory,
        org.pentaho.core.repository.ISolutionRepository"
     %>
    <%!
    private static final Log logger = LogFactory.getLog(String.class);
    public Log getLogger() {
    return logger;
    }
    private int index = 0;
    private String basepath;
    %>
    <%
            System.out.println("------------START LOAD XML------------");
            long start = java.util.Calendar.getInstance().getTimeInMillis();
    response.setCharacterEncoding(LocaleHelper.getSystemEncoding());
    response.setContentType("text/xml");

    HttpSession httpSession = request.getSession();
    IPentahoSession userSession = UIUtil.getPentahoSession( request );
    HttpRequestParameterProvider requestParameters = new HttpRequestParameterProvider(request);
    HttpSessionParameterProvider sessionParameters = new HttpSessionParameterProvider(userSession);
    String baseUrl = PentahoSystem.getApplicationContext().getBaseUrl();

    httpSession.setAttribute( "dopentahosplash", "false" );
    String hrefUrl = baseUrl; //$NON-NLS-1$
    String onClick = ""; //$NON-NLS-1$
    String thisUrl = baseUrl + "SelectXML?"; //$NON-NLS-1$
    basepath = request.getContextPath();
    SimpleUrlFactory urlFactory = new SimpleUrlFactory(thisUrl);
    ArrayList messages = new ArrayList();
        
    String solution = request.getParameter("solution"); //$NON-NLS-1$
    if(solution==null|| "".equals( solution ) ) { //$NON-NLS-1$
    solution = null;
    }
    String path= request.getParameter( "path" );
    if(path ==null || "".equals( path )){
    path =null;
    } boolean allowBackNavigation = solution != null;
    INavigationComponent navigate = NavigationComponentFactory.getNavigationComponent(); navigate.setHrefUrl(hrefUrl);
    navigate.setOnClick(onClick);
    navigate.setSolutionParamName("solution");
    navigate.setPathParamName("path");
    navigate.setAllowNavigation(new Boolean(allowBackNavigation));
    navigate.setOptions("");
    navigate.setUrlFactory(urlFactory);
    navigate.setMessages(messages);
    navigate.validate( userSession, null );
    navigate.setParameterProvider( HttpRequestParameterProvider.SCOPE_REQUEST, requestParameters ); //$NON-NLS-1$
    navigate.setParameterProvider( HttpSessionParameterProvider.SCOPE_SESSION, sessionParameters ); //$NON-NLS-1$

    String view = request.getParameter("view");//$NON-NLS-1$
    if( view != null ) {
    if( "default".equals( view ) ) { //$NON-NLS-1$
    userSession.removeAttribute( "pentaho-ui-folder-style" ); //$NON-NLS-1$
    } else {
    userSession.setAttribute( "pentaho-ui-folder-style", view );
    navigate.setXsl( "text/html", view ); //$NON-NLS-1$
    }
    } else {
    view = (String) userSession.getAttribute( "pentaho-ui-folder-style" );
    if( view != null ) {
    navigate.setXsl( "text/html", view ); //$NON-NLS-1$
    }
    }

    Document doc=navigate.getXmlContent();
    //查看源xml参数
    String viewSource = request.getParameter( "viewSource" ); //$NON-NLS-1$
    if("1".equals(viewSource)){
      response.setContentType("text/xml");
      response.getWriter().write(doc.asXML());   
      return;
    }
    Document newdoc=null;
    try {
    if(solution ==null){
      newdoc = getHomeXml(doc, false);
    }else if( solution !=null){
    newdoc = getHomeXml(doc, true);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    String content=newdoc.asXML();
    long end = java.util.Calendar.getInstance().getTimeInMillis();
    //System.out.println("this procedure cost "+(end-start)+" ms");
    //System.out.println("------------END LOAD XML------------");
    //System.out.println("+XML+"+content);
    response.getWriter().write(content);
    %>
    <%! public Document getHomeXml(Document oldDoc, boolean solutionFlag)
    throws Exception {
    Document doc = DocumentHelper.createDocument();
    Element root = oldDoc.getRootElement();
    if (solutionFlag) {
    root = (Element) root.selectNodes("file").get(0);
    }
    Element newroot = doc.addElement("xmlmenu"); if (solutionFlag
    || root.attributeValue("visible").equalsIgnoreCase("true")) {
    List elements = root.selectNodes("file", "title",false); Element element = null; for (int i = 0; i < elements.size(); i++) {

    String solution = "";
    String path = "";
    String filename = "";
    String url = "";
    String id;
    String type;
    String title;
    element = (Element) elements.get(i);
    if (element.attributeValue("visible").equalsIgnoreCase("true")) {
    Element menu = newroot.addElement("menu");
    id = "" + (index++);
    type = element.attributeValue("type");
    title = element.element("title").getText();
    if (element.attributeValue("type").equalsIgnoreCase(
    "FILE.ACTIVITY")) {

        solution=element.element("solution").getText();
           filename=element.element("filename").getText();
           path=element.element("path").getText();
    url = basepath + "/ViewAction?solution=" + solution
    + "&path=" + path + "&action=" + filename;
    } else if (element.attributeValue("type").equalsIgnoreCase(
    "FILE.URL")) {
    url = element.element("url").getText();
    } else if (element.attributeValue("type").equalsIgnoreCase(
    "FILE.FOLDER")) {
    solution = element.element("solution").getText();
    filename = element.attributeValue("name");
    path = element.attributeValue("path");
    }
    menu.addAttribute("id",id);
    menu.addAttribute("filename",filename);
    menu.addAttribute("type",type);
    menu.addAttribute("path",path);
    menu.addAttribute("text",title);
    menu.addAttribute("url",url);
    }
    } } return doc;
    }
    %>
      

  3.   

    这个是解析文件,只能解析到第二层,第三层后的元素都不能解析,麻烦各位大哥大姐帮帮忙。
    <%@page
    language="java"
    pageEncoding="GBK"%>
    <%@ page import="java.io.*,
        java.util.*,
    javax.servlet.ServletException,
    javax.servlet.http.HttpServletRequest,
    javax.servlet.http.HttpServletResponse,
    javax.servlet.http.HttpSession,
    javax.xml.parsers.DocumentBuilder,
    javax.xml.parsers.DocumentBuilderFactory,
    javax.xml.parsers.ParserConfigurationException,org.apache.commons.logging.Log,
    org.apache.commons.logging.LogFactory,org.dom4j.Document,
    org.dom4j.DocumentException,
    org.dom4j.DocumentHelper,
    org.dom4j.Element,
    org.pentaho.core.session.IPentahoSession,
    org.pentaho.core.solution.HttpRequestParameterProvider,
    org.pentaho.core.solution.HttpSessionParameterProvider,
    org.pentaho.core.system.PentahoSystem,
    org.pentaho.core.ui.SimpleUrlFactory,
    org.pentaho.core.util.UIUtil,
    org.pentaho.messages.util.LocaleHelper,
    org.pentaho.ui.component.INavigationComponent,
    org.pentaho.ui.component.NavigationComponentFactory,
        org.pentaho.core.repository.ISolutionRepository"
     %>
    <%!
    private static final Log logger = LogFactory.getLog(String.class);
    public Log getLogger() {
    return logger;
    }
    private int index = 0;
    private String basepath;
    %>
    <%
            System.out.println("------------START LOAD XML------------");
            long start = java.util.Calendar.getInstance().getTimeInMillis();
    response.setCharacterEncoding(LocaleHelper.getSystemEncoding());
    response.setContentType("text/xml");

    HttpSession httpSession = request.getSession();
    IPentahoSession userSession = UIUtil.getPentahoSession( request );
    HttpRequestParameterProvider requestParameters = new HttpRequestParameterProvider(request);
    HttpSessionParameterProvider sessionParameters = new HttpSessionParameterProvider(userSession);
    String baseUrl = PentahoSystem.getApplicationContext().getBaseUrl();

    httpSession.setAttribute( "dopentahosplash", "false" );
    String hrefUrl = baseUrl; //$NON-NLS-1$
    String onClick = ""; //$NON-NLS-1$
    String thisUrl = baseUrl + "SelectXML?"; //$NON-NLS-1$
    basepath = request.getContextPath();
    SimpleUrlFactory urlFactory = new SimpleUrlFactory(thisUrl);
    ArrayList messages = new ArrayList();
        
    String solution = request.getParameter("solution"); //$NON-NLS-1$
    if(solution==null|| "".equals( solution ) ) { //$NON-NLS-1$
    solution = null;
    }
    String path= request.getParameter( "path" );
    if(path ==null || "".equals( path )){
    path =null;
    } boolean allowBackNavigation = solution != null;
    INavigationComponent navigate = NavigationComponentFactory.getNavigationComponent(); navigate.setHrefUrl(hrefUrl);
    navigate.setOnClick(onClick);
    navigate.setSolutionParamName("solution");
    navigate.setPathParamName("path");
    navigate.setAllowNavigation(new Boolean(allowBackNavigation));
    navigate.setOptions("");
    navigate.setUrlFactory(urlFactory);
    navigate.setMessages(messages);
    navigate.validate( userSession, null );
    navigate.setParameterProvider( HttpRequestParameterProvider.SCOPE_REQUEST, requestParameters ); //$NON-NLS-1$
    navigate.setParameterProvider( HttpSessionParameterProvider.SCOPE_SESSION, sessionParameters ); //$NON-NLS-1$

    String view = request.getParameter("view");//$NON-NLS-1$
    if( view != null ) {
    if( "default".equals( view ) ) { //$NON-NLS-1$
    userSession.removeAttribute( "pentaho-ui-folder-style" ); //$NON-NLS-1$
    } else {
    userSession.setAttribute( "pentaho-ui-folder-style", view );
    navigate.setXsl( "text/html", view ); //$NON-NLS-1$
    }
    } else {
    view = (String) userSession.getAttribute( "pentaho-ui-folder-style" );
    if( view != null ) {
    navigate.setXsl( "text/html", view ); //$NON-NLS-1$
    }
    }

    Document doc=navigate.getXmlContent();
    //查看源xml参数
    String viewSource = request.getParameter( "viewSource" ); //$NON-NLS-1$
    if("1".equals(viewSource)){
      response.setContentType("text/xml");
      response.getWriter().write(doc.asXML());   
      return;
    }
    Document newdoc=null;
    try {
    if(solution ==null){
      newdoc = getHomeXml(doc, false);
    }else if( solution !=null){
    newdoc = getHomeXml(doc, true);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    String content=newdoc.asXML();
    long end = java.util.Calendar.getInstance().getTimeInMillis();
    //System.out.println("this procedure cost "+(end-start)+" ms");
    //System.out.println("------------END LOAD XML------------");
    //System.out.println("+XML+"+content);
    response.getWriter().write(content);
    %>
    <%! public Document getHomeXml(Document oldDoc, boolean solutionFlag)
    throws Exception {
    Document doc = DocumentHelper.createDocument();
    Element root = oldDoc.getRootElement();
    if (solutionFlag) {
    root = (Element) root.selectNodes("file").get(0);
    }
    Element newroot = doc.addElement("xmlmenu"); if (solutionFlag
    || root.attributeValue("visible").equalsIgnoreCase("true")) {
    List elements = root.selectNodes("file", "title",false); Element element = null; for (int i = 0; i < elements.size(); i++) {

    String solution = "";
    String path = "";
    String filename = "";
    String url = "";
    String id;
    String type;
    String title;
    element = (Element) elements.get(i);
    if (element.attributeValue("visible").equalsIgnoreCase("true")) {
    Element menu = newroot.addElement("menu");
    id = "" + (index++);
    type = element.attributeValue("type");
    title = element.element("title").getText();
    if (element.attributeValue("type").equalsIgnoreCase(
    "FILE.ACTIVITY")) {

        solution=element.element("solution").getText();
           filename=element.element("filename").getText();
           path=element.element("path").getText();
    url = basepath + "/ViewAction?solution=" + solution
    + "&path=" + path + "&action=" + filename;
    } else if (element.attributeValue("type").equalsIgnoreCase(
    "FILE.URL")) {
    url = element.element("url").getText();
    } else if (element.attributeValue("type").equalsIgnoreCase(
    "FILE.FOLDER")) {
    solution = element.element("solution").getText();
    filename = element.attributeValue("name");
    path = element.attributeValue("path");
    }
    menu.addAttribute("id",id);
    menu.addAttribute("filename",filename);
    menu.addAttribute("type",type);
    menu.addAttribute("path",path);
    menu.addAttribute("text",title);
    menu.addAttribute("url",url);
    }
    } } return doc;
    }
    %>