필터 지우기
필터 지우기

Reading values from xml file

조회 수: 267 (최근 30일)
Chris
Chris 2012년 3월 26일
답변: Jeffrey Warner 2024년 4월 2일
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

답변 (6개)

Pritom Kumar Saha
Pritom Kumar Saha 2021년 8월 3일
Try
S = readstruct("abcd.xml")
it works on my matlab version.
It should work on matlab 2020b or higher version.
  댓글 수: 2
Adam Campos
Adam Campos 2022년 1월 18일
Holy moly that just saved me hours and hours of trying to figure this stuff out. Thank you!
Hongyuan Zhang
Hongyuan Zhang 2022년 2월 24일
This works pretty well!

댓글을 달려면 로그인하십시오.


Turlough Hughes
Turlough Hughes 2021년 5월 14일
You can now read XML files in R2021a using readtable (and readtimetable). For example:
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
Goncalo Gouveia
Goncalo Gouveia 2023년 1월 12일
simple and easy, worked like a charm
Catherine
Catherine 2023년 6월 10일
Agreed, this is exactly what I was looking for. I was getting nowhere with xmlread. Thank you very much.

댓글을 달려면 로그인하십시오.


Thomas
Thomas 2012년 3월 27일
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'
  댓글 수: 4
Thomas
Thomas 2012년 3월 27일
sorry, it should read new=xml2struct('sample.xml')
and then new.catalog.book{1,2}.price
Chris
Chris 2012년 3월 27일
Used your code: Still getting
Attempt to reference field of non-structure array.
Error in Calcium2Pextended>pushbutton1_Callback (line 96)
test_data.PVScan.Sequence.Frame{1,2}.Attributes.relativeTime

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2012년 3월 27일
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
Chris
Chris 2012년 3월 27일
I've tried xmlread(), I can get it to read the file but then I don't know how to access the relativeTime value
Image Analyst
Image Analyst 2012년 3월 27일
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
Chris 2012년 3월 27일
<?xml version="1.0" encoding="utf-8"?>
<PVScan version="4.0.0.53" date="3/23/2012 12:30:30 PM" notes="">
<Lasers>
<Laser name="Pockels" index="0" />
</Lasers>
<Sequence type="TSeries Timed Element" cycle="1" xYStageGridDefined="False" xYStageGridNumXPositions="0" xYStageGridNumYPositions="0" xYStageGridOverlapPercentage="10" xYStageGridXOverlap="10" xYStageGridYOverlap="10">
<Frame relativeTime="0" absoluteTime="9.104738" index="1" label="CurrentSettings">
<File channel="1" channelName="Ch1" preAmpID="0" filename="slice1_field1_500uA_stimB(+)-001_Cycle001_CurrentSettings_Ch1_000001.tif" />
<File channel="4" channelName="Spot" preAmpID="0" filename="slice1_field1_500uA_stimB(+)-001_Cycle001_CurrentSettings_Ch4_000001.tif" />
<ExtraParameters validData="True" />
<Key key="objectiveLens" permissions="Read, Write, Save" value="10X" />
<Key key="objectiveLensNA" permissions="Read, Write, Save" value="0.3" />
<Key key="objectiveLensMag" permissions="Read, Write, Save" value="10" />
<Key key="pixelsPerLine" permissions="Read, Write, Save" value="200" />
<Key key="linesPerFrame" permissions="Read, Write, Save" value="88" />
<Key key="systemType" permissions="Write, Save" value="0" />
<Key key="binningMode" permissions="Read, Write, Save" value="0" />
<Key key="frameAveraging" permissions="Read, Write, Save" value="0" />
<Key key="framePeriod" permissions="Read, Write, Save" value="0.191488" />
<Key key="scanlinePeriod" permissions="Read, Write, Save" value="0.002176" />
<Key key="dwellTime" permissions="Read, Write, Save" value="8" />
<Key key="bitDepth" permissions="Read, Write, Save" value="12" />
<Key key="positionCurrent_XAxis" permissions="Write, Save" value="19401.38671875" />
<Key key="positionCurrent_YAxis" permissions="Write, Save" value="-20993.3203125" />
<Key key="positionCurrent_ZAxis" permissions="Write, Save" value="15902.42" />
<Key key="zDevice" permissions="Write, Save" value="0" />
<Key key="rotation" permissions="Read, Write, Save" value="20" />
<Key key="opticalZoom" permissions="Read, Write, Save" value="1" />
<Key key="micronsPerPixel_XAxis" permissions="Read, Write, Save" value="0.219298245614035" />
<Key key="micronsPerPixel_YAxis" permissions="Read, Write, Save" value="0.219298245614035" />
<Key key="pmtGain_0" permissions="Write, Save" value="786.290322580645" />
<Key key="pmtGain_1" permissions="Write, Save" value="0" />
<Key key="pmtOffset_0" permissions="Write, Save" value="0" />
<Key key="pmtOffset_1" permissions="Write, Save" value="0" />
<Key key="laserPower_0" permissions="Write, Save" value="18" />
<Key key="laserPowerCalibrated_0" permissions="Write, Save" value="0" />
<Key key="laserPowerAttenuation_0" permissions="Write, Save" value="0" />
<Key key="twophotonLaserPower_0" permissions="Write, Save" value="2926.17" />
<Key key="minVoltage_XAxis" permissions="Write, Save" value="-0.205995077821132" />
<Key key="minVoltage_YAxis" permissions="Write, Save" value="-0.855391548993729" />
<Key key="maxVoltage_XAxis" permissions="Write, Save" value="0.722359696628628" />
<Key key="maxVoltage_YAxis" permissions="Write, Save" value="0.0375257076868368" />
<Key key="imagingDevice" permissions="Write, Save" value="Ultima" />
</Frame>
<Frame relativeTime="0.191488" absoluteTime="9.296226" index="2" label="CurrentSettings">
<File channel="1" channelName="Ch1" preAmpID="0" filename="slice1_field1_500uA_stimB(+)-001_Cycle001_CurrentSettings_Ch1_000002.tif" />
<File channel="4" channelName="Spot" preAmpID="0" filename="slice1_field1_500uA_stimB(+)-001_Cycle001_CurrentSettings_Ch4_000002.tif" />
<ExtraParameters validData="True" />
<Key key="objectiveLens" permissions="Read, Write, Save" value="10X" />
<Key key="objectiveLensNA" permissions="Read, Write, Save" value="0.3" />
<Key key="objectiveLensMag" permissions="Read, Write, Save" value="10" />
<Key key="pixelsPerLine" permissions="Read, Write, Save" value="200" />
<Key key="linesPerFrame" permissions="Read, Write, Save" value="88" />
<Key key="systemType" permissions="Write, Save" value="0" />
<Key key="binningMode" permissions="Read, Write, Save" value="0" />
<Key key="frameAveraging" permissions="Read, Write, Save" value="0" />
<Key key="framePeriod" permissions="Read, Write, Save" value="0.191488" />
<Key key="scanlinePeriod" permissions="Read, Write, Save" value="0.002176" />
<Key key="dwellTime" permissions="Read, Write, Save" value="8" />
<Key key="bitDepth" permissions="Read, Write, Save" value="12" />
<Key key="positionCurrent_XAxis" permissions="Write, Save" value="19401.38671875" />
<Key key="positionCurrent_YAxis" permissions="Write, Save" value="-20993.3203125" />
<Key key="positionCurrent_ZAxis" permissions="Write, Save" value="15902.42" />
<Key key="zDevice" permissions="Write, Save" value="0" />
<Key key="rotation" permissions="Read, Write, Save" value="20" />
<Key key="opticalZoom" permissions="Read, Write, Save" value="1" />
<Key key="micronsPerPixel_XAxis" permissions="Read, Write, Save" value="0.219298245614035" />
<Key key="micronsPerPixel_YAxis" permissions="Read, Write, Save" value="0.219298245614035" />
<Key key="pmtGain_0" permissions="Write, Save" value="786.290322580645" />
<Key key="pmtGain_1" permissions="Write, Save" value="0" />
<Key key="pmtOffset_0" permissions="Write, Save" value="0" />
<Key key="pmtOffset_1" permissions="Write, Save" value="0" />
<Key key="laserPower_0" permissions="Write, Save" value="18" />
<Key key="laserPowerCalibrated_0" permissions="Write, Save" value="0" />
<Key key="laserPowerAttenuation_0" permissions="Write, Save" value="0" />
<Key key="twophotonLaserPower_0" permissions="Write, Save" value="2926.17" />
<Key key="minVoltage_XAxis" permissions="Write, Save" value="-0.205995077821132" />
<Key key="minVoltage_YAxis" permissions="Write, Save" value="-0.855391548993729" />
<Key key="maxVoltage_XAxis" permissions="Write, Save" value="0.722359696628628" />
<Key key="maxVoltage_YAxis" permissions="Write, Save" value="0.0375257076868368" />
<Key key="imagingDevice" permissions="Write, Save" value="Ultima" />
</Frame>
</Sequence>
</PVScan>
  댓글 수: 6
Chris
Chris 2012년 3월 27일
I think it may have to do with the 655K filesize. I can get the proper return value when I only include a few frames in the xml file...
Chris
Chris 2012년 3월 27일
Figured it out.
Had to use xpath, but could jump right to the node I wanted without mapping from the file (which was too big). Explanation here http://blogs.mathworks.com/desktop/2010/11/01/xml-and-matlab-navigating-a-tree/
[xmlfilename,pathname]=uigetfile('*.xml');
openxmlfilename = fullfile(pathname,xmlfilename);
xDoc=xmlread(openxmlfilename)
import javax.xml.xpath.*
factory = XPathFactory.newInstance;
xpath = factory.newXPath;
expression = xpath.compile('PVScan/Sequence/Frame[2]/@relativeTime');
relativeTime = expression.evaluate(xDoc, XPathConstants.NUMBER)

댓글을 달려면 로그인하십시오.


Jeffrey Warner
Jeffrey Warner 2024년 4월 2일
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>

카테고리

Help CenterFile Exchange에서 Structured Data and XML Documents에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by