Reading values from xml file
Afficher commentaires plus anciens
I've read around a bit and cant seem to get this to work. I am trying to read out a specific value from stereotyped xml files. The xml files look like this: http://i.imgur.com/uSEQt.png and I am trying to access the second "Frame relativeTime" value from the file for use in a data analysis pack I'm writing. There are many "Frame realtive time" entrees in the xml file but I am just trying to pull the second value and save it as a variable."
Any thoughts?
Thanks
Réponses (6)
Pritom Kumar Saha
le 3 Août 2021
7 votes
Try
S = readstruct("abcd.xml")
it works on my matlab version.
It should work on matlab 2020b or higher version.
2 commentaires
Adam Campos
le 18 Jan 2022
Holy moly that just saved me hours and hours of trying to figure this stuff out. Thank you!
Hongyuan Zhang
le 24 Fév 2022
This works pretty well!
Turlough Hughes
le 14 Mai 2021
filename = 'students.xml';
T = readtable(filename);
You can also select specific variables or attributes to read via detectImportOptions (or XMLImportOptions) - first get the opts and display the variable names:
opts = detectImportOptions(filename);
opts.VariableNames.'
then select the variable names that you want - let's say you want FirstNameAttribute, LastNameAttribute and Age, that would go as follows:
opts.SelectedVariableNames = opts.VariableNames(2:4);
T = readtable(filename,opts)
2 commentaires
Goncalo Gouveia
le 12 Jan 2023
simple and easy, worked like a charm
Catherine
le 10 Juin 2023
Agreed, this is exactly what I was looking for. I was getting nowhere with xmlread. Thank you very much.
Thomas
le 27 Mar 2012
Try this from the file exchange. I have found it to be useful
example : here is sample.xml
<?xml version="1.0"?>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
If I have to find the price of the second book:
c=xml2struct('sample.xml') % use function from file exchange
>> new.catalog.book{1,2}.price
ans =
Text: '5.95'
Image Analyst
le 27 Mar 2012
1 vote
Did you try xmlread()? Can you show your code and maybe upload your xml file and m-file somewhere so people can try something if they want to?
2 commentaires
Chris
le 27 Mar 2012
Image Analyst
le 27 Mar 2012
Did you try it with the example code they gave? You know, all the "function theStruct = parseXML(filename)" stuff. It's complicated I know but that seems like the way they recommend in the help.
Chris
le 27 Mar 2012
6 commentaires
Geoff
le 27 Mar 2012
Could you please format that as code? Edit, highlight all, hit the '{} code' button, save. Cheers.
Chris
le 27 Mar 2012
Thomas
le 27 Mar 2012
Chris I saved your xml file as test.xml
>>test_data=xml2struct('test.xml')
to get the second relativeTime
>> test_data.PVScan.Sequence.Frame{1,2}.Attributes.relativeTime
ans =
0.191488
Chris
le 27 Mar 2012
Chris
le 27 Mar 2012
Chris
le 27 Mar 2012
Jeffrey Warner
le 2 Avr 2024
0 votes
Hello,
I am trying to read this xml file to return the current filter position (<FilterPosition>) which is 4, and the Position name and position ID under the <Position Defined> node. I have not dealt with xm; files before. I tried xmlread and that returned a null result. Any healp is greatly appreciated.
DOMnode = xmlread(flName).
DOMnode = [#document: null]
I could not upload the xml file. Here is the contents of the file.
<FW102CProfileModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Pcount="6">
<SpeedMode>HighSpeed</SpeedMode>
<TriggerMode>Input</TriggerMode>
<SensorMode>TurnOff</SensorMode>
<FilterPosition>4</FilterPosition>
<PositionDefined>
<FW102CFilterConfigMode>
<PositionName>OD0</PositionName>
<PositionID>1</PositionID>
</FW102CFilterConfigMode>
<FW102CFilterConfigMode>
<PositionName>OD0.2 (5232)</PositionName>
<PositionID>2</PositionID>
</FW102CFilterConfigMode>
<FW102CFilterConfigMode>
<PositionName>OD0.5 (5235)</PositionName>
<PositionID>3</PositionID>
</FW102CFilterConfigMode>
<FW102CFilterConfigMode>
<PositionName>OD1 (5240)</PositionName>
<PositionID>4</PositionID>
</FW102CFilterConfigMode>
<FW102CFilterConfigMode>
<PositionName>OD2 (5242)</PositionName>
<PositionID>5</PositionID>
</FW102CFilterConfigMode>
<FW102CFilterConfigMode>
<PositionName>OD2.5 (5243)</PositionName>
<PositionID>6</PositionID>
</FW102CFilterConfigMode>
</PositionDefined>
<SequenceDefined/>
</FW102CProfileModel>
Catégories
En savoir plus sur Structured Data and XML Documents dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!