1

我目前正在尝试使用 Apache Digester 使用来自某些 XML 的字符串列表,如如何将文字元素添加到 List 对象中所述?常见问题解答部分。

我遇到以下错误:

[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]);

我几乎是使用 Digester 的新手,而且我很难找到我需要的示例。

谁能看到我哪里出错了?

4

1 回答 1

1

嗯,这个问题为我赢得了“风滚草”徽章,我正在努力寻找某种方法来重新表述这个问题,以便更容易理解。所以这里是我的进度更新:

最后我决定放弃 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);

如果有人对这个问题有建议,我仍然很想知道为什么我在使用 Digester 时遇到这么多麻烦。

于 2010-07-19T09:10:50.280 回答