用消化器解析

发布于 2024-10-27 03:38:05 字数 103 浏览 4 评论 0原文

我的 xml 就像 value1 我想使用 commons-digester 解析它并希望将“value1”作为 String 对象获取

My xml is like <value>value1</value> i want to parse it using commons-digester and want to get "value1" as a String object

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

早乙女 2024-11-03 03:38:05

根据 Lukasz 的回答,如果您不想创建另一个对象来保存该值,那么您可以将 StringBuilder 推入堆栈,然后使用 append 调用 append代码>addCallMethod。为了清楚起见,我在这里重现了代码:

public class StringExtractor {
    public static void main(String[] args) throws IOException, SAXException {
        final String xml = "<value>value1</value>";

        final Digester digester = new Digester();
        digester.push(new StringBuilder());
        digester.addCallMethod("*/value", "append", 1);
        digester.addCallParam("*/value", 0);

        final String value = digester.parse(new StringReader(xml)).toString();
        System.out.println(value);
    }
}

Going with Lukasz answer, if you don't want to create another object to hold the value, then you can just push a StringBuilder onto the stack and then call append using addCallMethod. I've reproduced the code here for clarity:

public class StringExtractor {
    public static void main(String[] args) throws IOException, SAXException {
        final String xml = "<value>value1</value>";

        final Digester digester = new Digester();
        digester.push(new StringBuilder());
        digester.addCallMethod("*/value", "append", 1);
        digester.addCallParam("*/value", 0);

        final String value = digester.parse(new StringReader(xml)).toString();
        System.out.println(value);
    }
}
心病无药医 2024-11-03 03:38:05

我认为不可能将“value1”直接加载到 String 实例中(通过 addObjectCreate),因为 String 是不可变的。相反,我会强制消化器调用一个方法。简短示例:

import java.io.File;
import org.apache.commons.digester.Digester;

public class Tester {
    public static void main(String[] args) throws Exception {
        Tester t = new Tester();
        t.go();
    }

    private String read;

    void go() throws Exception {
        Digester digester = new Digester();
        digester.push(this);
        digester.addCallMethod("value", "readString", 1);
        digester.addCallParam("value", 0);
        digester.parse(new File("tester.xml"));
        System.out.println("string: " + read );
    }

    public void readString(String a) {
        this.read = a;
    }
}

tester.xml

<?xml version="1.0" encoding="UTF-8"?>
<value>value1</value>

I don't think it is possible to load "value1" directly into String instance (by addObjectCreate), because String is immutable. Instead of this, I'd force digester to call a method. Short sample:

import java.io.File;
import org.apache.commons.digester.Digester;

public class Tester {
    public static void main(String[] args) throws Exception {
        Tester t = new Tester();
        t.go();
    }

    private String read;

    void go() throws Exception {
        Digester digester = new Digester();
        digester.push(this);
        digester.addCallMethod("value", "readString", 1);
        digester.addCallParam("value", 0);
        digester.parse(new File("tester.xml"));
        System.out.println("string: " + read );
    }

    public void readString(String a) {
        this.read = a;
    }
}

tester.xml:

<?xml version="1.0" encoding="UTF-8"?>
<value>value1</value>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文