DOM XML解析JAVA:从ChildNodes获取嵌入式标记

时间:2017-01-20 09:12:14

标签: java android xml-parsing domparser

我想从此xml feed

获取<img>中的<description>

这是一个Feed项的示例项:

<item>
  <title>Justizminister Maas: Auch Hausverkäufer sollen Makler bezahlen</title>
  <description>
      <img width=190 height=107 border=0 title="Heiko Maas auf dem Weg zur  
       wöchentlichen Kabinettssitzung im Kanzleramt." alt="Heiko Maas auf dem 
       Weg zur wöchentlichen Kabinettssitzung im Kanzleramt." 
       src=http://media0.faz.net/ppmedia/aktuell/653672970/1.4700918/article_teaser/heiko
       -maas-auf-dem-weg-zur.jpg />
     <p>Für Mietwohnungen gilt bereits: Wer den Makler 
        bestellt, muss ihn bezahlen. Justizminister Heiko Maas würde dieses 
        Prinzip gerne 
        auch beim Haus- und Wohnungskauf einführen.</p>
   </description>
   <link>http://www.faz.net/aktuell/wirtschaft/immobilien/justizminister-maas-auch-hausverkaeufer-sollen-makler-bezahlen-14700828.html</link>
   <pubDate>Thu, 19 Jan 2017 21:48:49 +0100</pubDate>
   <guid isPermaLink="true">http://www.faz.net/-gz7-8r38c</guid>
</item>

我在卡片视图中显示信息:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="4dp"
app:cardBackgroundColor="@color/cardview_dark_background">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:text="Date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pubDate"
        android:layout_below="@+id/desc"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:padding="10dp" />

    <TextView
        android:text="Beschreibung"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/desc"
        android:textColor="@color/colorAccent"
        android:padding="10dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/title" />

    <TextView
        android:text="GUID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pubDate"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/guid"
        android:padding="10dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:id="@+id/thumbnail"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:text="Title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/title"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
        android:textAlignment="center"
        android:textColor="@color/colorAccent"
        android:background="@color/colorPrimaryDark"
        android:lines="3"
        android:padding="5dp"
        android:alpha="0.8"
        android:layout_below="@+id/thumbnail"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

</android.support.v7.widget.CardView>`

我使用的on process process XML方法:

 private void processXML(Document dataSource) {
    if(dataSource!=null){
        feedItems = new ArrayList<>();
        Element root= dataSource.getDocumentElement();
        Node channel= root.getChildNodes().item(1);
        NodeList items = channel.getChildNodes();
        for(int i =0; i<items.getLength();i++){
            Node currentChild = items.item(i);
            if(currentChild.getNodeName().equalsIgnoreCase("item")){
                FeedItem item = new FeedItem();
                NodeList itemchilds = currentChild.getChildNodes();
                for (int j=0; j < itemchilds.getLength(); j++){
                    Node current = itemchilds.item(j);
                    if(current.getNodeName().equalsIgnoreCase("title")){
                        item.setTitle(current.getTextContent());
                    }else if(current.getNodeName().equalsIgnoreCase("description")){
                        item.setDescription(current.getTextContent());
                    }else if(current.getNodeName().equalsIgnoreCase("pubDate")){
                        item.setPubDate(current.getTextContent());
                    }else if(current.getNodeName().equalsIgnoreCase("guid")){
                        item.setGuid(current.getTextContent());
                    }

                    /*else if(current.getNodeName().equalsIgnoreCase("media:thumbnail")){
                        String url = current.getAttributes().item(0).getTextContent();
                        item.setThumbnailURL(url);
                    }*/
                }// end for-loop with j as variable
                feedItems.add(item);
            } // end if-loop item
        }// end for-loop with i as variable
    } // end if datasource
} // processXML

我知道如何查看Data,但我不清楚如何将<img><description>转换为我使用的缩略图和<p>嵌入<description> Textview desc中的private async void txtjsonname_DoubleClick(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "JSON Files (*.json)|*.json"; ofd.InitialDirectory = @"C:\"; ofd.Title = "Select single json file to be converted"; ofd.Multiselect = false; if (ofd.ShowDialog() == DialogResult.OK) { rtbstat.Text = null; txtcsvname.Text = null; txtjsonname.Text = null; lblcsvpath.Text = null; lbljsonpath.Text = null; rtbjson.Clear(); txtjsonname.Text = Path.GetFileName(ofd.FileName); lbljsonpath.Text = Path.GetDirectoryName(ofd.FileName); if (await LoadJSONtoRTB(ofd.FileName)) { rtbjson.WordWrap = false; rtbstat.Text = "Load file finished! " + (rtbjson.Lines.Count()).ToString() + " line(s) detected | " + rtbjson.Text.Length.ToString() + " character(s) detected"; txtcsvname.Text = Path.GetFileNameWithoutExtension(ofd.FileName) + ".csv"; } } await Task.Delay(1000); }

有没有办法可以做到这一点?或者它是不可能的,因为xml-feed格式不正确?感谢您的帮助和建议。

如果您需要更多信息,请询问。

0 个答案:

没有答案