用jsoup读取XML [英] Reading XML with jsoup

查看:85
本文介绍了用jsoup读取XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to java, and certainly new to jsoup. In this preliminary step of my program, I'm trying to get a web based XML file into an object I can start using to output my content. (It is a huge XML file, and I want to eventually be able to add filters)

这是一些示例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>

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

Here is my code so far.

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);
    }
}

我要这样做:

输出:酸飞溅,辅助物

它实际输出的内容: 输出:

What it actually Outputs: 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>

推荐答案

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

getElementsMatchingOwnText tries to find element based on its own text, like when you want to find <name>Foo Bar</name> based on Foo or Bar. Instead use

  • select支持CSS查询格式,
  • document.getElementsByTag("name")
  • select which supports CSS query format,
  • or document.getElementsByTag("name")

还可以实际获取表示调用e.text()的元素的文本.

Also to actually get text which element represent call e.text().

顺便说一句,您不应该通过串联来循环构建字符串.在每次迭代中,都需要通过复制旧结果(可能很长)来创建新字符串,并向其中添加一小部分.而是对它使用StringBuilderappend新内容(此类是很大尺寸的char[]数组的包装器,因此append仅用文本填充它,当数组的长度不够时,将其替换为两倍的array)尺寸).完成后,调用toString方法以String形式获取结果.

BTW you shouldn't be building strings in loop via concatenation. In each iteration this needs to create new string by copying old result (which can be long) and add some small part to it. Instead use StringBuilder and append new content to it (this class is wrapper for char[] array of quite big size so append just fills it with text, when length of array is not enough it is being replaced by array with doubled size). When you are done, call toString method to get result as String.

所以您想要的更像是

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

这篇关于用jsoup读取XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆