2010-11-23 8 views

答えて

1

Androidの独自のSAXパーサーにスピンを与えることもできます。私はそれを使用しようとし、それは素晴らしい動作します。これは、IBMサイトのサンプル・コードにも基づいています。

public class ParseXML { 

private URL feedUrl; 

public ParseXML(String urlSource){ 
    try { 
     this.feedUrl = new URL(urlSource); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 
} 


protected InputStream getInputStream() { 
    try { 
     return feedUrl.openConnection().getInputStream(); 
    } catch (IOException e) { 
     throw new RuntimeException(e); 
    } 
} 


public YourDataContainer parse(){ 
    YourDataContainer container = new YourDataContainer(); 

    RootElement root = new RootElement("root"); 

    Element firstChild = root.getChild("element1"); 
    firstChild.getChild("entry1").setEndTextElementListener(new EndTextElementListener() { 

     public void end(String body) { 
      container.setFirstEntry(body); 
     } 
    }); 
    firstChild.getChild("entry2").setEndTextElementListener(new EndTextElementListener() { 

     public void end(String body) { 
      container.setSecondEntry(body); 
     } 
    }); 


    try { 
     Xml.parse(this.getInputStream(), Xml.Encoding.ISO_8859_1, root.getContentHandler()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (SAXException e) { 
     e.printStackTrace(); 
    } 
    return container; 
} 

ここで、yourContainerには、保存したい値とセッターのフィールドがあります。

これは、このような解析します:実行の終わりに

<root> 
    <element1> 
     <entry1>this is</entry1> 
     <entry2>a test</entry2> 
    </element1> 
</root> 

を、YourContainerはfirstEntryは=「これは」とsecondEntry =「テスト」になります。

1

my answer hereにリンクします。それはあなたが始めるのを助けるはずです。基本については、ちょうどGoogle JavaのSAX解析です。

関連する問題