我是 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>
到目前为止,这是我的代码。
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);
}
}
我想要它做什么:
输出:酸液飞溅,援助
它实际输出的内容: 输出:
<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
尝试根据其自身的文本查找元素,例如当您要查找 <name>Foo Bar</name>
时基于 Foo
或 Bar
.而是使用
-
select
支持CSS查询格式, - 或
document.getElementsByTag("name")
还要实际获取元素代表调用的文本 e.text()
.
顺便说一句,您不应该通过连接在循环中构建字符串。在每次迭代中,这需要通过复制旧结果(可能很长)并向其中添加一些小部分来创建新字符串。而是使用 StringBuilder
和 append
它的新内容(此类是 char[]
相当大的数组的包装器,因此附加只是用文本填充它,当数组的长度不够时,它会被双倍大小的数组替换)。完成后,请调用 toString
方法以字符串形式获取结果。
所以你想要的更像
Elements elements = document.getElementsByTag("name");
StringBuilder sb = new StringBuilder();
for(Element e : elements) {
sb.append(e.text()).append(", ");
}
desc = sb.toString();
关于java - 使用 jsoup 读取 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44101791/