我使用 eclipse,我想挑选节点 mytag 文本。我应该怎么做才能得到它?我有以下代码:
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/outertag/innertag/mytag/text()";
InputSource inputSource = new InputSource("http://www.anddev.org/images/tut/basic/parsingxml/example.xml");
NodeList nodes = null;
try {
nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int i=nodes.getLength();
我参考的资料是http://www.w3schools.com/xpath/xpath_examples.asp 现在我check in debug的时候,给nodes的内容很复杂,但是没有一个是mytag text --anddev.org rulez 为了把这个拿出来,我该怎么办?谢谢!
该网站中的 xml 文件(URL 参数):
<?xml version="1.0"?>
<outertag>
<innertag sampleattribute="innertagAttribute">
<mytag>
anddev.org rulez =)
</mytag>
<tagwithnumber thenumber="1337"/>
</innertag>
</outertag>
结果 i 为 1。但节点不提供任何文本字符串。这是一个以 m 开头的“描述”列表。我想要的只是把那个文本拿出来。因为我真正需要解析的是一个内部网站,那是一个巨大的表格。我需要根据每个类别存储该表中的数据。例如,一个文件包含名称和数学分数,另一个包含名称和物理。
<TABLE border="2" frame="hsides" rules="groups"
summary="Code page support in different versions
of MS Windows.">
<CAPTION>CODE-PAGE SUPPORT IN MICROSOFT WINDOWS</CAPTION>
<COLGROUP align="center">
<COLGROUP align="left">
<COLGROUP align="center" span="2">
<COLGROUP align="center" span="3">
<THEAD valign="top">
<TR>
<TH>Code-Page<BR>ID
<TH>Name
<TH>ACP
<TH>OEMCP
<TH>Windows<BR>NT 3.1
<TH>Windows<BR>NT 3.51
<TH>Windows<BR>95
<TBODY>
<TR><TD>1200<TD>Unicode (BMP of ISO/IEC-10646)<TD><TD><TD>X<TD>X<TD>*
<TR><TD>1250<TD>Windows 3.1 Eastern European<TD>X<TD><TD>X<TD>X<TD>X
<TR><TD>1251<TD>Windows 3.1 Cyrillic<TD>X<TD><TD>X<TD>X<TD>X
<TR><TD>1252<TD>Windows 3.1 US (ANSI)<TD>X<TD><TD>X<TD>X<TD>X
<TR><TD>1253<TD>Windows 3.1 Greek<TD>X<TD><TD>X<TD>X<TD>X
<TR><TD>1254<TD>Windows 3.1 Turkish<TD>X<TD><TD>X<TD>X<TD>X
<TR><TD>1255<TD>Hebrew<TD>X<TD><TD><TD><TD>X
<TR><TD>1256<TD>Arabic<TD>X<TD><TD><TD><TD>X
<TR><TD>1257<TD>Baltic<TD>X<TD><TD><TD><TD>X
<TR><TD>1361<TD>Korean (Johab)<TD>X<TD><TD><TD>**<TD>X
<TBODY>
<TR><TD>437<TD>MS-DOS United States<TD><TD>X<TD>X<TD>X<TD>X
<TR><TD>708<TD>Arabic (ASMO 708)<TD><TD>X<TD><TD><TD>X
<TR><TD>709<TD>Arabic (ASMO 449+, BCON V4)<TD><TD>X<TD><TD><TD>X
<TR><TD>710<TD>Arabic (Transparent Arabic)<TD><TD>X<TD><TD><TD>X
<TR><TD>720<TD>Arabic (Transparent ASMO)<TD><TD>X<TD><TD><TD>X
</TABLE>
和之后的所有内容,所以我认为它必须是 nodeList 。