Finding nodes of nodes in an XML file

14 ビュー (過去 30 日間)
Saeed Soltani
Saeed Soltani 2016 年 7 月 28 日
回答済み: Robert Ungi 2022 年 1 月 5 日
How can I find nodes of element X just in element Y (not in whole of file) ?
<Y>
<X>
<Value>AirMass</Value>
<Value>RailPressure</Value>
<Value>BoostPressure</Value>
</X>
<Y>
<X>
<Value>Speed</Value>
<Value>Torque</Value>
</X>
What I have, returns the whole childs of X:
d = xmlread('myfile.xml')
v = d.getElementsByTagName('X')
for i=0:v.getLength-1
v.item(i).getTextContent
end
I just need to get: AirMass, RailPressure, BoostPressure.

回答 (1 件)

Robert Ungi
Robert Ungi 2022 年 1 月 5 日
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;
fXML=xmlread('SampleXML.xml');
expression = xpath.compile('xml/Y/X/Value');
Value = expression.evaluate(fXML, XPathConstants.NODESET);
for i=0:Value.getLength-1
disp(Value.item(i).getTextContent)
end
Adopted from The Wizzart.

カテゴリ

Help Center および File ExchangeStructured 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