【问题标题】:Reading xml tag atributes in android在android中读取xml标签属性
【发布时间】:2014-07-14 07:46:08
【问题描述】:

我正在开发一个 Android 应用程序,它使用来自服务器等外部来源的数据。使用 DOM 解析器检索数据。我的实际问题是

 <enclosure url="http://news.oneindia.in/img/2014/07/15-tripura-map-600.jpg" type="image/jpeg" length="810000"/>

我想从标签附件中检索我的应用程序的 url。

当我检索附件标签时,我得到空值。
那么我如何从标签内部获取这些数据呢?

<item>
  <title>Modi in Brazil, Israel airstrike: Follow News In Brief: July 15</title>
  <link>http://news.oneindia.in/india/follow-news-brief-july-15-1482982.html</link>
  <guid>http://news.oneindia.in/india/follow-news-brief-july-15-1482982.html</guid>
  <description>Bangalore, July 15: Follow news in brief for the day: 2:40 pm:&amp;nbsp;Lalu, Nitish to forge alliance for Bihar Assembly by-polls 2:22 pm:&amp;nbsp;World Cup 2014 champions German team arrives in home country, receives grand welcome 1:50 pm:&amp;nbsp;30 die of </description>
  <enclosure url="http://news.oneindia.in/img/2014/07/15-pm-narendramodi-brics.jpg" type="image/jpeg" length="810000"/>
  <pubDate>Tue, 15 Jul 2014 08:48:32 +0530</pubDate>
</item>
<item>
  <title>Heavy rain alert for Uttarakhand</title>
  <link>http://news.oneindia.in/india/heavy-rain-alert-uttarakhand-1482983.html</link>
  <guid>http://news.oneindia.in/india/heavy-rain-alert-uttarakhand-1482983.html</guid>
  <description>Lucknow/Dehradun, July 15: An alert has been sounded in Uttarakhand after incessant rains in many parts of the state for the last 48 hours, officials said Tuesday.The hill state is likely to experience very heavy rainfall Wednesday, the Met Office said. </description>
  <enclosure url="http://news.oneindia.in/img/2014/07/15-dehradun-map-600.jpg" type="image/jpeg" length="810000"/>
  <pubDate>Tue, 15 Jul 2014 08:48:06 +0530</pubDate>
</item>
<item>
  <title>Former US president Bill Clinton arrives in Jaipur</title>
  <link>http://news.oneindia.in/india/former-us-prez-bill-clinton-arrives-in-jaipur-1482985.html</link>
  <guid>http://news.oneindia.in/india/former-us-prez-bill-clinton-arrives-in-jaipur-1482985.html</guid>
  <description>Jaipur, July 15: Former US President Bill Clinton arrived here on Monday night on a tour during which he will visit a kitchen being run for schoolchildren by an NGO. Clinton landed with his delegation at the international airport at </description>
  <enclosure url="http://news.oneindia.in/img/2014/07/15-bill-clinton-latest.jpg" type="image/jpeg" length="810000"/>
  <pubDate>Tue, 15 Jul 2014 01:25:31 +0530</pubDate>
</item>

我想从上面的xml中提取附件内的内容。

这是我的java代码

    public class Second_Listview extends Activity {

private GestureDetector gestureDetector;
// All static variables
// static final String URL = "http://ibnlive.in.com/ibnrss/top.xml";
String URL;
// XML node keys
static final String KEY_SONG = "item"; // parent node
static final String KEY_ID = "description";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "pubDate";
static final String KEY_DURATION = "link";
static final String KEY_DIS = "description";
 static final String KEY_THUMB_URL = "enclosure";

ListView list;
LazyAdapter adapter;
String descrption;
String image;
String main_title;

ProgressBar progress;

String KEY_IMAGE;
String imgurl;


// >>> in case of data connection failure

// flag for Internet connection status
Boolean isInternetPresent = false;

// Connection detector class
ConnectionDetector cd;



@Override
public void onCreate(Bundle savedInstanceState) {


     gestureDetector = new GestureDetector(
             new SwipeGestureDetector());

    //reciving intent data

    Intent intent = getIntent();
     main_title = getIntent().getExtras().getString("title_key").trim();

    super.onCreate(savedInstanceState);
    setTitle(main_title);   // setting title bar title
    setContentView(R.layout.main);
    progress=(ProgressBar)findViewById(R.id.progressBarMain);
    progress.setVisibility(View.GONE);
    //reciving intent data



    String page_id = getIntent().getExtras().getString("webpage_key");  //reciving intent data

    URL = page_id.trim(); // trimming data to avoid space

    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL);// getting XML from URL

    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_SONG);
    // looping through all song nodes <song>
    for (int i = 0; i < nl.getLength(); i++) {


        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);

        descrption = parser.getValue(e, KEY_THUMB_URL);


        if (e.getNodeName().contains("enclosure") && e.hasAttributes()){
                imgurl= e.getAttribute("url").toString();


        }

        System.out.println("VALUE INSIDE>>>>>>>>>>> "+url+" <<<< DDDD"+ descrption );

        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
        map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
        map.put(KEY_DIS, parser.getValue(e, KEY_DIS));
        map.put(KEY_IMAGE, imgurl);

我在描述中得到的值为空

【问题讨论】:

  • 您使用什么代码来“检索附件标签”?仅通过阅读您的问题很难为您提供帮助。

标签: java android xml-parsing


【解决方案1】:

要获取附件标签的 url 属性的值,您可以执行与帖子中指定的类似的操作 Parsing attribute in XML with DOM parser。下面是一个示例。

NamedNodeMap attributes = fstElmnt.getAttributes();
Node enclosureItem = attributes.getNamedItem("url");

如果您发布了您的解析代码,那就太好了,然后可以发布您可以直接在代码中使用的示例。

==================== 根据您的代码编辑 ====================== ===

Element e = (Element) nl.item(i);

//if (e.getNodeName().contains("enclosure") && e.hasAttributes()){
   String url = e.getAttribute("url").toString();
//}

【讨论】:

  • 我刚刚更新了我的问题,详细解释,请给我一个好的解决方案
  • 感谢您的帮助,但它仍然在 url 中获得 Null 值。我该怎么办?
  • 你能更新你上面的代码,这样我就可以看到你更新的逻辑实现了吗?
  • 您是否遇到任何异常或错误?是否进入获取 url 属性值的 if 条件?
  • 没有错误或异常显示,我在 if 条件中放了一个 toast 但它不起作用。我认为 if 条件不起作用
猜你喜欢
  • 1970-01-01
  • 2016-04-30
  • 1970-01-01
  • 1970-01-01
  • 2018-01-19
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
  • 2017-02-17
相关资源
最近更新 更多