2015-01-08 2 views
1

xml 파일을 구문 분석하는 데 XOM을 사용하지만 문자열 이름으로 Element이 표시되는 문제가 있습니다. 나는 getChild(x)을 통해 반복하여 각 요소에 액세스 할 수 있는데, x=0:ChildCount이지만,이 작업을 수행하는 내장 함수가있는 소프트웨어 주위를 해킹해야한다는 것은 어리석은 것처럼 보입니다. 왜 NPE를 얻고 있습니까? 아래의 예제를보십시오 ... 저는 부모님이 내가 찾고있는 정확한 이름의 자녀를 포함하고 있는지 100 % 확신합니다. 이 문제를 해결하려면 어떻게해야합니까?XOM으로 XML 구문 분석시 Null 하위 요소

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> 

감사합니다!

답변

1

마지막으로 질문을 게시 한 후 알아 냈습니다. 왜 그런 일이 항상 일어나는 것 같습니까?

내가 필요한 것은 NameSpace 인수입니다. 대신 하드 코딩, 나는이었다 루트 노드의 네임 스페이스를 검색하고 그것의 아이들의 네임 스페이스를 가정 같은 : XOM이 다룰 수있을만큼 견고해야한다처럼

String nameSpace = root.getNamespaceURI(); 
// getChildElements 
Element player = root.getFirstChildElement("player",nameSpace); 
Element player_stats = player.getFirstChildElement("player_stats",nameSpace); 

보인다,하지만 난 그렇지 않은 가정 .

관련 문제