Java 在android中读取xml标记属性

Java 在android中读取xml标记属性,java,android,xml-parsing,Java,Android,Xml Parsing,我正在开发一个android应用程序,它使用来自服务器等外部来源的数据。使用DOM解析器检索数据。我的实际问题是 <enclosure url="http://news.oneindia.in/img/2014/07/15-tripura-map-600.jpg" type="image/jpeg" length="810000"/> 我想从标记附件中检索我的应用程序的url 当我检索机柜标签时,我得到空值。 那么我如何从标签内部获取数据呢 <item> &l

我正在开发一个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>

莫迪在巴西、以色列空袭:关注新闻摘要:7月15日
http://news.oneindia.in/india/follow-news-brief-july-15-1482982.html
http://news.oneindia.in/india/follow-news-brief-july-15-1482982.html
班加罗尔,7月15日:关注当天的简要新闻:下午2:40;nbsp;拉鲁,尼提什将在下午2:22投票前为比哈尔邦议会建立联盟:;nbsp;2014年世界杯冠军德国队抵达祖国,下午1:50受到隆重欢迎:;nbsp;三十死
2014年7月15日星期二08:48:32+0530
乌塔拉汗省暴雨警报
http://news.oneindia.in/india/heavy-rain-alert-uttarakhand-1482983.html
http://news.oneindia.in/india/heavy-rain-alert-uttarakhand-1482983.html
勒克瑙/德拉敦,7月15日:官员们周二表示,在过去48小时内,该州许多地区持续降雨后,北方邦已发出警报。气象局表示,该州周三可能会遭遇非常大的降雨。
2014年7月15日星期二08:48:06+0530
美国前总统克林顿抵达斋浦尔
http://news.oneindia.in/india/former-us-prez-bill-clinton-arrives-in-jaipur-1482985.html
http://news.oneindia.in/india/former-us-prez-bill-clinton-arrives-in-jaipur-1482985.html
斋浦尔,7月15日:美国前总统比尔·克林顿星期一晚上抵达这里,他将参观一家非政府组织为学童经营的厨房。克林顿和他的代表团于11月15日在国际机场降落
2014年7月15日星期二01:25:31+0530
我想从上面的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);
公共类第二个\u列表视图扩展活动{
私人手势检测器;
//所有静态变量
//静态最终字符串URL=”http://ibnlive.in.com/ibnrss/top.xml";
字符串URL;
//XML节点密钥
静态最终字符串键\u SONG=“item”//父节点
静态最终字符串键\u ID=“description”;
静态最终字符串键\u TITLE=“TITLE”;
静态最终字符串键\u ARTIST=“pubDate”;
静态最终字符串键\u DURATION=“link”;
静态最终字符串键\u DIS=“description”;
静态最终字符串键\u THUMB\u URL=“enclosure”;
列表视图列表;
懒散适配器;
字符串描述;
字符串图像;
字符串主标题;
进度条进度;
字符串键图像;
字符串imgurl;
//>>>在数据连接失败的情况下
//Internet连接状态的标志
布尔值isInternetPresent=false;
//连接检测器类
连接检测器cd;
@凌驾
创建时的公共void(Bundle savedInstanceState){
gestureDetector=新的gestureDetector(
新SwipeGestureDetector());
//接收意图数据
Intent=getIntent();
main_title=getIntent().getExtras().getString(“title_key”).trim();
super.onCreate(savedInstanceState);
setTitle(主标题);//设置标题栏标题
setContentView(R.layout.main);
进度=(ProgressBar)findViewById(R.id.progressBarMain);
progress.setVisibility(View.GONE);
//接收意图数据
String page_id=getIntent().getExtras().getString(“网页密钥”);//接收意图数据
URL=page_id.trim();//修剪数据以避免空格
ArrayList songsList=新的ArrayList();
XMLParser=新的XMLParser();
字符串xml=parser.getXmlFromUrl(URL);//从URL获取xml
Document doc=parser.getDomeElement(xml);//获取DOM元素
NodeList nl=doc.getElementsByTagName(KEY_-SONG);
//循环通过所有歌曲节点
对于(int i=0;iSystem.out.println(“内部值>>>>>>>>>>”+url+“要获取附件标记的url属性值,可以执行与文章中指定的类似操作
.以下是一个示例

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中仍然有空值。我该怎么办?你能更新上面的代码,让我看到你更新的逻辑实现吗?你有什么例外吗n或错误?是否在获取url属性值的if条件内?没有错误或异常显示,我在if条件内放置了一个toast,但它不起作用。我认为if条件不起作用