2012-07-26 62 views
1

如何解析android中的這個xml數據。Android xml樣式表解析

xml-stylesheet格式與正常xml不同嗎?

我需要這些字段;

KOD BanknoteBuying BanknoteSelling

<?xml version="1.0" encoding="ISO-8859-9"?> 
<?xml-stylesheet type="text/xsl" href="isokur.xsl"?> 
<Tarih_Date Tarih="20.07.2012" Date="07/20/2012"> 

<Currency Kod="USD" CurrencyCode="USD"> 
    <Unit>1</Unit> 
    <Isim>AMERİKAN DOLARI</Isim> 
    <CurrencyName>US DOLLAR</CurrencyName> 
    <ForexBuying>1.7967</ForexBuying> 
    <ForexSelling>1.8054</ForexSelling> 
    <BanknoteBuying>1.7954</BanknoteBuying> 
    <BanknoteSelling>1.8081</BanknoteSelling> 
    <CrossRateUSD>1</CrossRateUSD> 
    <CrossRateOther></CrossRateOther> 
</Currency> 

<Currency Kod="EUR" CurrencyCode="EUR"> 
    <Unit>1</Unit> 
    <Isim>EURO</Isim> 
    <CurrencyName>EURO</CurrencyName> 
    <ForexBuying>2.1998</ForexBuying> 
    <ForexSelling>2.2104</ForexSelling> 
    <BanknoteBuying>2.1983</BanknoteBuying> 
    <BanknoteSelling>2.2137</BanknoteSelling> 
    <CrossRateUSD></CrossRateUSD> 
    <CrossRateOther>1.2243</CrossRateOther> 
</Currency> 

回答

1

Android提供了大部分java的api(SAX & DOM)來解析XML文檔。你可以做下面的事情來解析XML並獲取你需要的字段。

DocumentBuilder docBuild= docBuildFactory.newDocumentBuilder(); 
InputSource in= new InputSource(); 
in.setCharacterStream(new StringReader(xml)); 
Document doc = docBuild.parse(in); 
NodeList nodes = doc.getElementsByTagName("Currency"); 
Element e = (Element)nodes.item(i); 
XMLfunctions.getValue(e, "BanknoteBuying") 

這必須解決您的問題.... :)

1

我不認爲它應該是不同的。最後,這也是一個由標籤和屬性組成的XML文件。
如果您需要刷新有關解析XML this post可能會讓你感到高興。

0

您可以放心地忽略xml-stylesheet processing instruction,並像處理任何其他XML一樣解析XML。 xml-stylesheet是一種暗示演示代理(如瀏覽器)的轉換,它將呈現XML,因爲它將被呈現。代理決定是否使用樣式表。

在這種情況下的代理是您的應用程序。

0

如果你真的想要的isokur.xsl XSLT樣式表被應用到你的XML,那麼我想你可能是出於運氣。據我所知,目前還沒有Android上的XSLT處理器。我們正在考慮將撒克遜移植到這個平臺上的可能性,但它今天不存在。