使用 XOM 解析 XML 时为空子元素
Null Child Element when Parsing XML with XOM
我正在使用 XOM 解析 xml 文件,但我在通过字符串名称获取 Element
时遇到问题。我可以通过遍历 getChild(x)
和 x=0:ChildCount
来访问每个元素,但是在有内置函数来完成此任务的软件中绕过我的方法似乎很愚蠢。为什么我会得到 NPE?请参阅下面的示例...我 100% 确定父项包含一个具有我正在搜索的确切名称的子项。我能做些什么来解决这个问题?
FileInputStream xmlFile = new FileInputStream("temp.xml");
Builder builder = new Builder();
Document doc = builder.build(xmlFile);
Element root = doc.getRootElement();
System.out.println(((Element)root.getChild(1)).getLocalName()); //--> prints "player"
Element player = root.getFirstChildElement(((Element)root.getChild(1)).getLocalName()); //--> null
System.out.println(player); //--> prints "null"
Element player_stats = player.getFirstChildElement("player_stats"); //--> NPE
temp.xml
<?xml version="1.0" encoding="UTF-8"?>
<fantasy_content xml:lang="en-US"
yahoo:uri="http://fantasysports.yahooapis.com/fantasy/v2/player/nfl.p.7206/stats;type=week;week=2"
time="34.230947494507ms" copyright="Data provided by Yahoo! and STATS, LLC"
refresh_rate="31" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
xmlns="http://fantasysports.yahooapis.com/fantasy/v2/base.rng">
<player>
<player_key>331.p.7206</player_key>
<player_id>7206</player_id>
<name>
<full>Heath Miller</full>
<first>Heath</first>
<last>Miller</last>
<ascii_first>Heath</ascii_first>
<ascii_last>Miller</ascii_last>
</name>
<injury_note>not injury related</injury_note>
<editorial_player_key>nfl.p.7206</editorial_player_key>
<editorial_team_key>nfl.t.23</editorial_team_key>
<editorial_team_full_name>Pittsburgh Steelers</editorial_team_full_name>
<editorial_team_abbr>Pit</editorial_team_abbr>
<bye_weeks>
<week>12</week>
</bye_weeks>
<uniform_number>83</uniform_number>
<display_position>TE</display_position>
<is_undroppable>0</is_undroppable>
<position_type>O</position_type>
<eligible_positions>
<position>TE</position>
</eligible_positions>
<has_player_notes>1</has_player_notes>
<player_stats>
<coverage_type>week</coverage_type>
<week>2</week>
<stats>
<stat>
<stat_id>0</stat_id>
<value>1</value>
</stat>
<stat>
<stat_id>1</stat_id>
<value>0</value>
</stat>
<stat>
<stat_id>2</stat_id>
<value>0</value>
</stat>
<stat>
<stat_id>3</stat_id>
<value>0</value>
</stat>
...
<stat>
<stat_id>81</stat_id>
<value>0</value>
</stat>
</stats>
</player_stats>
</player>
</fantasy_content>
谢谢!
发布问题后不久,终于弄明白了。为什么这似乎总是发生?
我需要的是 NameSpace 参数。我没有对它进行硬编码,而是检索了根节点的命名空间,并假设它的子节点的命名空间是相同的:
String nameSpace = root.getNamespaceURI();
// getChildElements
Element player = root.getFirstChildElement("player",nameSpace);
Element player_stats = player.getFirstChildElement("player_stats",nameSpace);
似乎 XOM 应该足够强大来处理这个问题,但我想它不是。
我正在使用 XOM 解析 xml 文件,但我在通过字符串名称获取 Element
时遇到问题。我可以通过遍历 getChild(x)
和 x=0:ChildCount
来访问每个元素,但是在有内置函数来完成此任务的软件中绕过我的方法似乎很愚蠢。为什么我会得到 NPE?请参阅下面的示例...我 100% 确定父项包含一个具有我正在搜索的确切名称的子项。我能做些什么来解决这个问题?
FileInputStream xmlFile = new FileInputStream("temp.xml");
Builder builder = new Builder();
Document doc = builder.build(xmlFile);
Element root = doc.getRootElement();
System.out.println(((Element)root.getChild(1)).getLocalName()); //--> prints "player"
Element player = root.getFirstChildElement(((Element)root.getChild(1)).getLocalName()); //--> null
System.out.println(player); //--> prints "null"
Element player_stats = player.getFirstChildElement("player_stats"); //--> NPE
temp.xml
<?xml version="1.0" encoding="UTF-8"?>
<fantasy_content xml:lang="en-US"
yahoo:uri="http://fantasysports.yahooapis.com/fantasy/v2/player/nfl.p.7206/stats;type=week;week=2"
time="34.230947494507ms" copyright="Data provided by Yahoo! and STATS, LLC"
refresh_rate="31" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
xmlns="http://fantasysports.yahooapis.com/fantasy/v2/base.rng">
<player>
<player_key>331.p.7206</player_key>
<player_id>7206</player_id>
<name>
<full>Heath Miller</full>
<first>Heath</first>
<last>Miller</last>
<ascii_first>Heath</ascii_first>
<ascii_last>Miller</ascii_last>
</name>
<injury_note>not injury related</injury_note>
<editorial_player_key>nfl.p.7206</editorial_player_key>
<editorial_team_key>nfl.t.23</editorial_team_key>
<editorial_team_full_name>Pittsburgh Steelers</editorial_team_full_name>
<editorial_team_abbr>Pit</editorial_team_abbr>
<bye_weeks>
<week>12</week>
</bye_weeks>
<uniform_number>83</uniform_number>
<display_position>TE</display_position>
<is_undroppable>0</is_undroppable>
<position_type>O</position_type>
<eligible_positions>
<position>TE</position>
</eligible_positions>
<has_player_notes>1</has_player_notes>
<player_stats>
<coverage_type>week</coverage_type>
<week>2</week>
<stats>
<stat>
<stat_id>0</stat_id>
<value>1</value>
</stat>
<stat>
<stat_id>1</stat_id>
<value>0</value>
</stat>
<stat>
<stat_id>2</stat_id>
<value>0</value>
</stat>
<stat>
<stat_id>3</stat_id>
<value>0</value>
</stat>
...
<stat>
<stat_id>81</stat_id>
<value>0</value>
</stat>
</stats>
</player_stats>
</player>
</fantasy_content>
谢谢!
发布问题后不久,终于弄明白了。为什么这似乎总是发生?
我需要的是 NameSpace 参数。我没有对它进行硬编码,而是检索了根节点的命名空间,并假设它的子节点的命名空间是相同的:
String nameSpace = root.getNamespaceURI();
// getChildElements
Element player = root.getFirstChildElement("player",nameSpace);
Element player_stats = player.getFirstChildElement("player_stats",nameSpace);
似乎 XOM 应该足够强大来处理这个问题,但我想它不是。