用jsoup读取XML

kyl*_*yle 1 java android jsoup

我是java的新手,并且肯定是jsoup的新手。在程序的此初步步骤中,我试图将基于Web的XML文件放入一个对象,我可以开始使用该文件输出内容。(这是一个巨大的XML文件,我希望最终能够添加过滤器)

这是一些示例XML。

<spell>
    <name>Acid Splash</name>
    <level>0</level>
    <school>C</school>
    <time>1 action</time>
    <range>60 feet</range>
    <components>V, S</components>
    <duration>Instantaneous</duration>
    <classes>Sorcerer, Wizard, Fighter (Eldritch Knight), Rogue (Arcane Trickster)</classes>
    <text>You hurl a bubble of acid. Choose one creature within range, or choose two creatures within range that are within 5 feet of each other. A target must succeed on a Dexterity saving throw or take 1d6 acid damage.</text>
    <text />
    <text>This spells damage increases by 1d6 when you reach 5th Level (2d6), 11th level (3d6) and 17th level (4d6).</text>
    <roll>1d6</roll>
    <roll>2d6</roll>
    <roll>3d6</roll>
    <roll>4d6</roll>
</spell>
<spell>
    <name>Aid</name>
    <level>2</level>
    <school>A</school>
    <time>1 action</time>
    <range>30 feet</range>
    <components>V, S, M (a tiny strip of white cloth)</components>
    <duration>8 hours</duration>
    <classes>Artificer, Cleric, Paladin</classes>
    <text>Your spell bolsters your allies with toughness and resolve. Choose up to three creatures within range. Each target's hit point maximum and current hit points increase by 5 for the duration.</text>
    <text />
    <text>At Higher Levels: When you cast this spell using a spell slot of 3rd level or higher, a target's hit points increase by an additional 5 for each slot level above 2nd.</text>
</spell>
Run Code Online (Sandbox Code Playgroud)

到目前为止,这是我的代码。

private class Description extends AsyncTask<Void, Void, Void> {
    String desc;



    @Override
    protected Void doInBackground(Void... params) {
        try {
            // Connect to the web site
            Document document = Jsoup.parse(new URL(url).openStream(), "UTF-8", "", Parser.xmlParser());
            Elements elements = document.getElementsMatchingOwnText("name");
            // Using Elements to get the Meta data
            for(Element e : elements) {
                desc = desc +", "+ e;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Set description into TextView
        TextView txtdesc = (TextView) findViewById(R.id.desctxt);
        txtdesc.setText(desc);
    }
}
Run Code Online (Sandbox Code Playgroud)

我要这样做:

输出:酸飞溅,辅助

它实际输出的内容:Output:

<text>
This spells damage increases by 1d6 when you reach 5th Level (2d6), 11th level (3d6) and 17th level (4d6).
</text>, <text>
At Higher Levels: When you cast this spell using a spell slot of 3rd level or higher, a target's hit points increase by an additional 5 for each slot level above 2nd.
</text>
Run Code Online (Sandbox Code Playgroud)

Psh*_*emo 5

getElementsMatchingOwnText尝试根据自己的文本查找元素,例如当您要<name>Foo Bar</name>基于Foo或查找元素时Bar。改为使用

  • select 支持CSS查询格式,
  • 要么 document.getElementsByTag("name")

还可以实际获取表示哪个元素代表call的文本e.text()

顺便说一句,您不应该通过串联来循环构建字符串。在每次迭代中,都需要通过复制旧结果(可能很长)来创建新字符串,并向其中添加一小部分。相反,使用StringBuilderappend新的内容给它(这个类是包装char[]相当大的大小的数组,以便追加只是文字填满它,当数组的长度不够它是由具有双倍大小的数组替换)。完成后,调用toString方法以String形式获取结果。

所以你想要的更像是

Elements elements = document.getElementsByTag("name");
StringBuilder sb = new StringBuilder();
for(Element e : elements) {
    sb.append(e.text()).append(", ");
}
desc = sb.toString();
Run Code Online (Sandbox Code Playgroud)