2015-08-16 38 views
0

希望有人能夠提供幫助,並解決其簡單問題。我的代碼在運行代碼的當前輸出下面。需要使用Sax XML解析從特定元素開始的XML

public void startElement(String uri, String localName , String qName, 
     Attributes attributes) throws SAXException { 

    if(!localName.matches("characterID")) { 
     String Primary = attributes.getValue("characterID"); 
     System.out.println("ROOAR:" + Primary); 

    } else { 
     System.out.println("ROOAR:" + localName); 
    } 


Output 
    ROOAR:null 
    ROOAR:null 
    ROOAR:null 
    ROOAR:null 
    ROOAR:92916469 
    ROOAR:95325415 
    ROOAR:95528725 
    ROOAR:null 

我想知道我可以具體如何startElement在指定的子元素。現在,它開始於根元素,但我寧願從第5行開始,因爲它有它自己的子元素。

示例XML

  <eveapi version="2"> 
      <currentTime>2015-08-14 22:44:09</currentTime> 
      <result> 
      <rowset name="characters" key="characterID" columns="name,characterID,corporationName,corporationID,allianceID,allianceName,factionID,factionName"> 
      <row name="Grasume" characterID="95528725" corporationName="School of Applied Knowledge" corporationID="1000044" allianceID="0" allianceName="" factionID="0" factionName="" /> 
      </rowset> 
      </result> 
      <cachedUntil>2015-08-14 23:03:33</cachedUntil> 
      </eveapi> 
+0

在您的問題中添加示例輸入。這有助於更好地理解你的問題。 –

回答

0

當使用基於事件的SAX解析,你必須保持跟蹤你當前的分析狀態,即深度或路徑,使用提供,即startElement的事件。

首先,跟蹤您輸入的「根」,然後輸入路徑中的各個級別,並在輸入您感興趣的路徑時,可以在此處處理屬性和子元素。