代码之家  ›  专栏  ›  技术社区  ›  seanhodges

在使用Apache Digester时,“对象:java.util.ArrayList上没有这样的可访问方法:setFields()”

  •  1
  • seanhodges  · 技术社区  · 15 年前

    我目前正在尝试使用Apache Digester使用一些XML中的字符串列表,如 How do I add literal elements to a List object? 常见问题部分。

    我遇到以下错误:

    [DEBUG] Digester - [SetNextRule]{job/editorial/articlegroup/article} Call java.util.ArrayList.setFields([This, This, is, is, a, a, test, test, , , , ])
    [ERROR] Digester - End event threw exception <java.lang.NoSuchMethodException: No such accessible method: setFields() on object: java.util.ArrayList>java.lang.NoSuchMethodException: No such accessible method: setFields() on object: java.util.ArrayList
    

    我使用的XML的简化版本如下:

    <job>
        <editorial>
            <articlegroup>
                <article>
                    <text>
                        <content><![CDATA[This]]></content>
                    </text>
                    <text>
                        <content><![CDATA[is]]></content>
                    </text>
                    <text>
                        <content><![CDATA[a]]></content>
                    </text>
                    <text>
                        <content><![CDATA[test]]></content>
                    </text>
                </article>
            </articlegroup>
        </editorial>
    </job>
    

    以及源代码:

    public class PPJob {
    
        List<String> fields;
    
        public List<String> getFields() {
            return fields;
        }
        public void setFields(List<String> fields) {
            this.fields = fields;
        }
    }
    
    
    addObjectCreate("job", PPJob.class);
    addSetProperties("job");
    
    addObjectCreate("job/editorial/articlegroup/article", ArrayList.class);
    addCallMethod("job/editorial/articlegroup/article/text/content", "add", 1);
    addCallParam("job/editorial/articlegroup/article/text/content", 0);
    addSetNext("job/editorial/articlegroup/article", "setFields");
    
    PPJob result = (PPJob)super.parse([THE XML]);
    

    我基本上是一个使用消化器的新手,我很难找到我需要的例子。

    1 回复  |  直到 14 年前
        1
  •  1
  •   seanhodges    15 年前

    好吧,这个问题为我赢得了“风滚草”徽章,我正在努力找到一些方法来重新描述这个问题,以便更容易理解。以下是我的最新进展:

    我最终决定放弃Commons Digester,由于时间限制,很难进一步解决这个问题,因此我没有在Digester项目中记录错误(如果其他人告诉我,我会分享我的经验)。

    javax XPath函数对于实现我的需求更为直接,我决定采用以下解决方案:

    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    rootQuery = xPath.compile("/job");
    textFieldsQuery = xPath.compile("/job/editorial/articlegroup/article/text|/job/editorial/articlegroup/article/flashtext");
    
    Node rootNode = (Node)rootQuery.evaluate(new InputSource(is), XPathConstants.NODE);
    
    PPJob job = new PPJob();
    Map<String, String> jobTextFields = new HashMap<String, String>(); 
    NodeList fields = (NodeList)query.evaluate(rootNode, XPathConstants.NODESET);
    for (int i = 0; i < fields.getLength(); i++) {
        Node field = fields.item(i);
        String fieldName = field.getAttributes().getNamedItem("name").getNodeValue();
        String fieldContent = field.getNextSibling().getNodeValue();
        jobTextFields.put(fieldName, fieldContent);
    }       
    job.setTextFields(jobTextFields);