设置属性值

时间:2014-03-20 16:48:08

标签: java xml sax

如何在创建xml文件期间将值设置为id属性,其中id值可以是任何值,例如它可以是id =" 1"或id =" A"或id =" k1",id =" k2"

public void process(String s) throws SAXException {

    try {

        atts.clear();
        k++;
        atts.addAttribute("", "", "id", "", "" + k);
        th.startElement("", "", "trans-unit", atts);
        th.startElement("", "", "target", null);
        th.characters(elements[i].toCharArray(), 0, elements[i].length());
        th.endElement("", "", "target");
        th.endElement("", "", "trans-unit");

     }

1 个答案:

答案 0 :(得分:0)

我假设您复制了适用于OP的this question代码,因此您的问题必须在其他地方。请提供更多关于失败的信息。

另外,请看一下这个easier implementation,也许这可以帮到你。

修改

您只需要更改addAttribute的最后一个参数

atts.addAttribute("", "", "id", "", yourStringOfChoiceHere);

addArttibute的文档是here。如果您计划在代码中保留进程方法,则可能需要将id值添加为变量

public void process(String s, String idValue) throws SAXException {

...
    atts.addAttribute("", "", "id", "", idValue);
...

}
相关问题