返回顶部

收藏

Velocity 解析 XML 数据

更多

这段代码用来在Velocity模板中遍历XML数据

XMLTest.java

/*
 * Copyright 2000,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.Template;
import org.apache.velocity.app.Velocity;

import org.jdom.Document;
import org.jdom.input.SAXBuilder;

/**
 * Example to show basic XML handling in a template.
 *
 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
 * @version $Id: XMLTest.java,v 1.2.12.1 2004/03/04 00:18:30 geirm Exp $
 */
public class XMLTest
{
    public XMLTest( String templateFile)
    {    
        Writer writer = null;

        try
        {
            /*
             *  and now call init
             */

            Velocity.init();

            /*
             * build a Document from our xml
             */

            SAXBuilder builder;
            Document root = null;

            try 
            {
                builder = new SAXBuilder(  "org.apache.xerces.parsers.SAXParser" );
                root = builder.build("./src/test.xml");
            }
            catch( Exception ee)
            { 
                System.out.println("Exception building Document : " + ee);
                return;
            }

            /*
             * now, make a Context object and populate it.
             */

            VelocityContext context = new VelocityContext();
            context.put("root", root);

            /*
             *  make a writer, and merge the template 'against' the context
             */

            Template template = Velocity.getTemplate(templateFile);

            writer = new BufferedWriter(new OutputStreamWriter(System.out));
            template.merge( context , writer);  
        }
        catch( Exception e )
        {
           System.out.println("Exception : " + e);
        }
        finally
        {
            if ( writer != null)
            {
                try
                {
                    writer.flush();
                    writer.close();
                }
                catch( Exception ee )
                {
                    System.out.println("Exception : " + ee );
                }
            }
        }
    }

    public static void main(String[] args)
    {
        XMLTest t;

        t = new XMLTest("./src/xml.vm");
    }
}

test.xml

<?xml version="1.0" encoding="UTF-8"?>
<document>
  <properties>
    <author>
  <email>velocity-user@jakarta.apache.org</email>
  <name>
     <last>
    <full>Team</full>
    <firstinitial>T</firstinitial>
     </last>
     <first>
    Velocity
     </first>
  </name>
    </author>
    <title>
        The Jakarta Project
    </title>
  </properties>

  <body>
  I am the body
  </body>
</document>

xml.vm

#macro ( recursive $e $indent )     
    #if( $e.getChildren().size() > 0 )
        $indent <$e.getName()> 
        #foreach ($child in $e.getChildren() ) 
            #recursive( $child "$indent  " )
        #end
        $indent </$e.getName()>
    #else
        $indent <$e.getName()>
        $indent    $e.getTextTrim()
        $indent </$e.getName()>
    #end
#end

#set($i = " ")

First, we print out the document tree with a 
recursive Velocimacro :

#recursive( $root.getRootElement() $i )

Next, we access pieces of data directly :

email : $root.getRootElement().getChild("properties").
    getChild("author").getChild("email").getText()

last name :  $root.getRootElement().getChild("properties").getChild("author").
    getChild("name").getChild("last").getChild("full").getText()

标签:jQuery,XML,Velocity

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2015-12-04 09:30:42Velocity 输出 XML 数据 by amaker8
  2. 2015-03-29 11:34:48将数组转换为XML by 杨德伟
  3. 2015-08-06 10:27:46将 Scala XML 转成 Java DOM by huwei
  4. 2015-12-06 11:04:09SAXParser 解析 XML 时禁用外部 DTD 加载 by starstroll
  5. 2015-04-09 14:34:03PHP中的生成XML文件的4种方法 by starstroll
  6. 2015-12-07 09:12:16JDOM: 输出命名空间属性 (Namespaces Attributes) by starstroll
  7. 2016-01-28 09:29:50JAXB marshling by 冬夜微风
  8. 2016-02-02 09:41:44JDOM: Properties To XML by GauSir
  9. 2015-09-15 11:58:45表格的读取 by 永明
  10. 2015-12-18 09:03:41使用 Digester 解析 XML 文件的一个实例 by huwei
  11. 2018-11-19 19:43:1824 game by 杨德伟
相关聚客文章
  1. Hiro 发表 2009-07-10 03:52:23 jQuery:书籍展示效果
  2. 歪脖骇客 发表 2016-05-05 03:53:37 使用jQuery解析XML
  3. Hiro 发表 2009-09-21 03:33:02 XPath:语法及示例,前端学习收集
  4. tiegen.zhutg 发表 2013-02-28 10:58:00 jQuery选择器探讨进阶
  5. lixiphp 发表 2013-03-13 15:30:53 PHP解析XML格式的Plist文件
  6. 王子墨 发表 2012-04-18 18:16:48 jQuery api 1.7 , jQuery api 1.7.1 chm
  7. Mars 发表 2012-08-06 05:49:03 50个精心收集的惊人的jquery绚丽插件–功能全覆盖
  8. 张映 发表 2013-01-07 07:54:46 jquery json 解析功能
  9. xmouse 发表 2013-03-27 01:11:17 jQuery视频教程10: JQuery中的动画与效果
  10. hqlulu <hqlulu@163.c 发表 2013-04-09 15:02:04 转:jquery的表单提交
  11. maisui99 发表 2012-07-31 06:25:47 Zepto.js之旅(一)
  12. zqalyc 发表 2013-05-09 07:33:38 请教jQuery-File-Upload实现无刷新页面上传图片

发表评论