有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java集合属性值

在创建xml文件期间,如何将值设置为id属性,其中id值可以是任何内容,例如它可以是 id="1" or id="A" or 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) 个答案

  1. # 1 楼答案

    我假设您是从this question复制代码的,该代码适用于OP,因此您的问题一定在别处。请提供有关失败原因的更多信息

    另外,看看这个easier implementation,也许这可以帮助你

    编辑

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

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

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

    public void process(String s, String idValue) throws SAXException {
    
    ...
        atts.addAttribute("", "", "id", "", idValue);
    ...
    
    }