2016-04-17 2 views
1

Ich muss Angebotsdatum, Kursart und Kurs anzeigen. Ich benutze Knoten, aber ich bin mir nicht sicher, ob es ein Element oder ein Attribut ist, es kann ein Namespace sein, nicht sicher, wie das zu referenzieren ist.Wie analysiere ich das XML-Tag <m: properties> für meine Android-Anwendung mit Java?

ist dies die XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<feed xml:base="http://data.treasury.gov/Feed.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom"> 
    <title type="text">DailyTreasuryLongTermRateData</title> 
    <id>http://data.treasury.gov/feed.svc/DailyTreasuryLongTermRateData</id> 
    <updated>2016-04-16T00:05:04Z</updated> 
    <link rel="self" title="DailyTreasuryLongTermRateData" href="DailyTreasuryLongTermRateData" /> 
    <entry> 
    <id>http://data.treasury.gov/Feed.svc/DailyTreasuryLongTermRateData(11440)</id> 
    <title type="text"></title> 
    <updated>2016-04-16T00:05:04Z</updated> 
    <author> 
     <name /> 
    </author> 
    <link rel="edit" title="DailyTreasuryLongTermRateDatum" href="DailyTreasuryLongTermRateData(11440)" /> 
    <category term="TreasuryDataWarehouseModel.DailyTreasuryLongTermRateDatum" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
    <content type="application/xml"> 
     <m:properties> 
     <d:Id m:type="Edm.Int32">11440</d:Id> 
     <d:QUOTE_DATE m:type="Edm.DateTime">2015-04-01T00:00:00</d:QUOTE_DATE> 
     <d:EXTRAPOLATION_FACTOR>N/A</d:EXTRAPOLATION_FACTOR> 
     <d:RATE_TYPE>BC_20year</d:RATE_TYPE> 
     <d:RATE m:type="Edm.Double">2.23</d:RATE> 
     </m:properties> 
    </content>`enter code here` 

dies mein Code so weit:

@Override 
    protected void onPostExecute(String result) { 

     DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder documentBuilder = null; 
     org.w3c.dom.Document doc = null; 
     try { 
      documentBuilder = documentBuilderFactory.newDocumentBuilder(); 
      InputSource is = new InputSource(new StringReader(result)); 
      doc = documentBuilder.parse(is); 
      doc.normalize(); 


      Node rootNode = doc.getDocumentElement(); 
      String rootNodeName = rootNode.getNodeName(); 
      tvNodeName.setText(rootNodeName); 

      NodeList nlProperties = doc.getElementsByTagName("d:"); 
      Node nProperties = nlProperties.item(0); 
      Node nPropertiesText = nProperties.getFirstChild(); 
      tvQuoteDate.setText(nPropertiesText.getNodeValue()); 

      NodeList nlPoint = doc.getElementsByTagName("d:"); 
      Node nPoint = nlPoint.item(0); 
      NamedNodeMap nnm = nPoint.getAttributes(); 

      Node rateType, rate; 
      rateType = nnm.getNamedItem("RATE_TYPE"); 
      rate = nnm.getNamedItem("RATE"); 

      tvRateType.setText(rateType.getNodeValue()); 
      tvRate.setText(rate.getNodeValue()); 


     } catch (Exception e) { 

     } 
    } 

Antwort

0

Ich schlage vor, Sie Einfach zu bedienen - XML-Bibliothek 3rd-Party.

http://simple.sourceforge.net/home.php. 

Überprüfen Sie ihre zweite Seite gibt es ein Tutorial und Code-Schnipsel.

Sie können "m" als Präfix verwenden und es fügt das "m" vor den Eigenschaften hinzu.

dies aus ihrer Tutorial-Seite genommen, stellen Sie sicher, dass Sie es dort zu lesen !:

@Namespace(reference="your refrence", prefix="m") 

Um Ihnen das Leben ein bisschen einfacher Sie auch converting_xml_to_pojo Websites versuchen zu machen und es wird für Sie erstellen Klassen automatisch, Sie müssen nur pase kopieren und fügen Sie die Elemente/Attribute zusätzliche Informationen wie @Element, @Attribute, etc, wie in ihrem Tutorial

erwähnt
Verwandte Themen