我正在尝试从XML文件中获取一些数据,但是我得到的是空数据,它似乎正在发现有3个结果,但它返回Null 3次而不是relivant数据。
XML如下
<eveapi version="2">
<currentTime>2014-03-10 11:12:27</currentTime>
<result>
<rowset name="characters" key="characterID" columns="name,characterID,corporationName,corporationID">
<row name="Char 1" characterID="0000000" corporationName="Test Corp" corporationID="000000"/>
<row name="Char 3" characterID="0000000" corporationName="Test Corp" corporationID="000000"/>
<row name="Char 2" characterID="0000000" corporationName="Test Corp" corporationID="000000"/>
</rowset>
</result>
<cachedUntil>2014-03-10 11:40:26</cachedUntil>
</eveapi>
抓取XML的代码
public static void APIGetChar(){
....
Element RootElement = document.getRootElement() ;
Element windowManager = RootElement.getChild("result")
.getChild("rowset");
List namedChildren = windowManager.getChildren("row");
for (int i = 0; i < namedChildren.size(); i++) {
Element node = (Element) namedChildren.get(i);
String rowset = node.getChildText("row name");
String row = node.getChildText("row");
String result = node.getChildText("result");
String result1 = node.getChildText("name");
System.out.println("Row Name: "+rowset);
System.out.println("Row: "+row);
System.out.println("Name: "+result1);
....
正如你所看到的,我尝试了不同的名字,甚至将它与我的其他XML抓取器进行了比较,我唯一的猜测是RowSet的处理方式不同,但没有多少googleing会得到答案,所以任何帮助都会很棒
我需要从XML中获取数据,并将其传递给字符串,以便我可以在程序中显示结果
答案 0 :(得分:1)
因此看起来该行集中的项目实际上是属性,因此当获取ChildText不起作用时,我使用了
String name = node.getAttributeValue("name");
允许我获取名称,所有其他代码都是正确的。
xml attribute
Web definitions
In XML, a designation that represents a combination of a name and value and is used to
provide additional information about an XML element, for example, <FONT SIZE=2>. In this
case, FONT is an element and SIZE=2 is an attribute.