In MATLAB, I load an XML file docNode = xmlread('stuff.xml');. stuff.xml is the following:
<?xml version="1.0"?>
<rss version="2.0" xmlns:g="http://somesite.com">
<channel>
<title>Blah</title>
<link>http://www.blah.com</link>
<description>BLAH.COM </description>
<item>
<link>http://www.blah.com/page</link>
</item>
</channel>
</rss>
I'm trying to retrieve that string in <link> but it is proving to be quite tricky.. I'm reading this blog http://blogs.mathworks.com/desktop/2010/11/01/xml-and-matlab-navigating-a-tree/ but I still can't figure it out! Can someone chime in on how to get access to <link>? TIA!
Does this do what you need?
>> docNode = xmlread('stuff.xml');
>> l = docNode.getElementsByTagName('link');
>> char(l.item(0).getFirstChild.getData)
ans =
http://www.blah.com
>> char(l.item(1).getFirstChild.getData)
ans =
http://www.blah.com/page
PS you have an error in stuff.xml - it should be </channel>, not </<channel>.
Edit: To loop directly through each link, you can use l.getLength:
for i = 0:(l.getLength - 1) % 0-based indexing, not 1-based like MATLAB arrays
char(l.item(i).getFirstChild.getData)
end
ans =
http://www.blah.com
ans =
http://www.blah.com/page
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With