Finding nodes of nodes in an XML file

25 views (last 30 days)
Saeed Soltani
Saeed Soltani on 28 Jul 2016
Answered: Robert Ungi on 5 Jan 2022
How can I find nodes of element X just in element Y (not in whole of file) ?
What I have, returns the whole childs of X:
d = xmlread('myfile.xml')
v = d.getElementsByTagName('X')
for i=0:v.getLength-1
I just need to get: AirMass, RailPressure, BoostPressure.

Answers (1)

Robert Ungi
Robert Ungi on 5 Jan 2022
Its a bit late, but whoever is looking for such a solution (I altered the smaple xml file above because it is invalid):
% <xml>
% <Y>
% <X>
% <Value>AirMass</Value>
% <Value>RailPressure</Value>
% <Value>BoostPressure</Value>
% </X>
% </Y>
% <X>
% <Value>Speed</Value>
% <Value>Torque</Value>
% </X>
% </xml>
import javax.xml.xpath.*
factory = XPathFactory.newInstance;
import javax.script.ScriptContext;
xpath = factory.newXPath;
expression = xpath.compile('xml/Y/X/Value');
Value = expression.evaluate(fXML, XPathConstants.NODESET);
for i=0:Value.getLength-1
Adopted from The Wizzart.



Community Treasure Hunt

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

Start Hunting!

Translated by