import org.jsoup.Jsoup;
import org.jsoup.nodes.*;
import org.jsoup.select.*;
public class My_Test {
public static void main(String[] args) throws Exception {
String xml = "<span id=sectionLinesDetail>\n" +
" <tr id=123>\n" +
" <td>text</td>\n" +
" </tr>\n" +
"</span>";
Document doc = Jsoup.parse(xml);
Elements e_span = doc.select("span[id=sectionLinesDetail]");
System.out.println(e_span);
}
}
我想要这样的结果:
< span id=sectionLinesDetail> < tr id=123> < td>text< /td> < /tr> < /span>
但是我得到的是这样的
< span id=sectionLinesDetail> text < /span>
是否有办法跳过验证?
谢谢。
最佳答案
一个Xml Parser
这就是您所需要的。
您只需将解析行更改为:
Document doc = Jsoup.parse(xml, "", Parser.xmlParser());
我对你的代码做了一些更改,但问题的重点只是这一行 - 其他所有内容都是装饰性的。
String xml = "<span id=sectionLinesDetail>\n"
+ " <tr id=\"123\">\n"
+ " <td>text</td>\n"
+ " </tr>\n"
+ "</span>";
Document doc = Jsoup.parse(xml, "", Parser.xmlParser()); // The line as mentioned above
Element span = doc.select("span#sectionLinesDetail").first(); // the '#' means "with id"
System.out.println(span);
输出:
<span id="sectionLinesDetail">
<tr id="123">
<td>text</td>
</tr> </span>
关于java - jsoup 解析而不验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16705976/