有没有办法在 XML 资源中定义自定义标签 [英] Is there a way to define custom tags in XML resources

查看:47
本文介绍了有没有办法在 XML 资源中定义自定义标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在项目 res 文件夹中定义类似的内容:

I would like to define something like this in the project res folder:

<custommap name="MyMap">
    <entry key="@string/key1" value="1">K1</entry >
    <entry key="@string/key2" value="2">K2</entry >
    <entry key="@string/key3" value="3">K3</entry >
    <entry key="@string/key4" value="4">K4</entry >
</custommap>

有什么可能吗?上面的例子没有通过编译(我试图将它插入到 arrays.xml 中).如果可以的话,如何从java代码中访问?

Is it somehow possible?. The example above does not pass compilation (I tried to insert it in arrays.xml). If possible, how can it be accessed from the java code?

推荐答案

  1. 通过右键单击 res 文件夹并创建您自己的 xml 文件在/xml 文件夹下创建一个新文件.

  1. Create your own xml file by right-clicking on the res folder and creating a new file under /xml folder.

将你的 xml 读入 ArrayList使用 XMLParser 如下,通过查找 XML 标签:

Read your xml into ArrayList using the XMLParser as follows, by looking for the XML tags:

public ArrayList<textElement> PrepareListFromXml(String cat)
{
    textElement te;

    ArrayList<textElement> listItems = new ArrayList<textElement>();
    XmlResourceParser listXml = getResources().getXml(R.xml.<your resource name>);

    int eventType = -1;
    while (eventType != XmlResourceParser.END_DOCUMENT)
    {
        if (eventType == XmlResourceParser.START_TAG)
        {                
            String strNode = listXml.getName();
            if (strNode.equals("entry"))
            {
                te.setText(listXml.getAttributeValue(null, "key"));
                listItems.add(te);
            }
        }

        try
        {
             eventType = listXml.next();
        }
        catch (XmlPullParserException e)
        {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
        catch (IOException e)
        {
               // TODO Auto-generated catch block
                e.printStackTrace();
        }
    }

    return listItems;
}

这篇关于有没有办法在 XML 资源中定义自定义标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆