Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an XML file like this :

XML
<item>  
  <title>...</title>  
  <link>...</link>    
  <pubDate>...</pubDate>
  <description>...</description>
  <media:content url=" ....." type="image/jpg" medium="image" height="270" width="480" />
</item>


I want to get attribute url in media:content tag.
But I don't know how to do this?
I only get <title>, <link>, <pubDate>, <description> using DOM Parser.

Please help me! Thanks and best regard!!!
Posted

The media:content node name means that this node is part of the XML namespace which is currently using the alias "media".

Somewhere in your document, you should see the namespace declaration:
XML
<someNode xmlns:media="..."

(If you don't, then the XML is malformed.)

According to this SO thread[^], you should call setNamespaceAware(true) on your DocumentBuilderFactory object, and use the getElementsByTagNameNS method to read the elements.
 
Share this answer
 
This is not an Android issue but an XML issue. The XML element you are trying to reference contains a namespace i.e. media. You need to import the XML namespace before you can reference it.

I'm not entirely sure how you achieve this in Java (I'm assuming you are doing this in Java) but in C# the code would be similar to the following.

XNamespace nsMedia = "media"; //the actual namespace may be at the top of the XML document
foreach (XElement xItem in xItems.Elements("item"))
{
  XElement xContent = xItem.Element(nsMedia + "content"); 
  XAttribute xUrl = xContent.Attribute("url");
  string url = xUrl.Value;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900