help using Xpath and XML reading

15 visualizzazioni (ultimi 30 giorni)
Jim O'Doherty
Jim O'Doherty il 18 Gen 2013
Dear all,
I'm recently new to using Matlab to navigate through an XML file, and have a feeling theres a simple answer to my query starting me in the face. I have an XML file which contains coordinates of 3 objects that come from a different program. I'm trying to import the coordinates of these objects and associated image number (identified by integer) into Matlab. I'm able to find the coordinates of each object individually, but would anyone know of a loop I could use as other XML files are a few hundred entries long
Heres my Matlab code so far:
import javax.xml.xpath.*
factory = XPathFactory.newInstance;
xpath = factory.newXPath;
expression = xpath.compile('plist/dict/array/dict/array');
coordinate_node = expression.evaluate(docNode, XPathConstants.STRING)
I tried the following, which does find 3 instances of "<array>" but simply outputs the same set of coordinates 3 times.
expression = xpath.compile('plist/dict/array/dict/array');
nodeList = expression.evaluate(docNode, XPathConstants.NODESET)
for i = 1:nodeList.getLength
node = nodeList.item(i-1);
coordinate_node{i} = expression.evaluate(docNode, XPathConstants.STRING)
end
And below is the XML file:
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<key>ROI array</key>
<array>
<dict>
<key>Comments</key>
<string></string>
<key>Name</key>
<string>Unnamed</string>
<key>ROIPoints</key>
<array>
<string>{129.24051549947484, 263.66036033996352}</string>
<string>{114.61421850240453, 278.56760216125258}</string>
<string>{123.11826208150609, 289.73859978088149}</string>
</array>
<key>Slice</key>
<integer>58</integer>
<key>Comments</key>
<string></string>
<key>Name</key>
<string>Unnamed</string>
<key>ROIPoints</key>
<array>
<string>{127.09352448499425, 261.31629753478774}</string>
<string>{112.50917389905675, 277.25509453185805}</string>
<string>{126.061969309213, 291.36980247863539}</string>
<string>{141.48499634778722, 292.16234398254164}</string>
<string>{149.71229126966222, 277.81281090148696}</string>
</array>
<key>Slice</key>
<integer>59</integer>
<key>Comments</key>
<string></string>
<key>Name</key>
<string>Unnamed</string>
<key>ROIPoints</key>
<array>
<string>{134.32833430087788, 258.21743274101027}</string>
<string>{117.0812182120107, 266.44891620048293}</string>
<string>{114.41427180087788, 292.20427203544386}</string>
<string>{128.80573603427632, 299.11905932792433}</string>
<string>{147.92307612216695, 299.11905932792433}</string>
<string>{152.73700281894429, 285.80526996024855}</string>
<string>{154.32626673495992, 268.51202655204543}</string>
</array>
<key>Slice</key>
<integer>60</integer>
</array>
</dict>
</plist>
Any help greatly appreciated!
Jim

Risposta accettata

Jarrod Rivituso
Jarrod Rivituso il 2 Feb 2013
I may be misunderstanding things, but I think you may want to try doing something like this:
dictExpr = xpath.compile('//dict');
allDict = dictExpr.evaluate(docNode, XPathConstants.NODESET)
Then, loop over the each "dict" element and get all the strings within an "array" element
stringExpr = xpath.compile('array//string')
for i = 1:allDict.getLength
currentDict = allDict.item(i-1);
stringNodesInCoord = stringExpr.evaluate(currentDict, XPathConstants.NODESET)
end
Also, I'd be interested in hearing what you think of this tool I made (I've dabbled with making xpath easier in MATLAB)
Oh, and I think there is an error in your initial code that you copied. I think
coordinate_node{i} = expression.evaluate(docNode, XPathConstants.STRING)
should be
coordinate_node{i} = expression.evaluate(node , XPathConstants.STRING)
Hope this helps!
-Jarrod

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by