Accessing XML node information

10 ビュー (過去 30 日間)
Jan Knop
Jan Knop 2023 年 3 月 17 日
コメント済み: Jan Knop 2023 年 3 月 17 日
I am trying to create a struct with information extracted from a XML file. The XML file is generated in a toolchain. The XML has the following format:
<Geometry_AC>
<fuselages>
<fuselage ID="0" Name="fuselage">
<XSections>
<XSec ID="0" name="sec0">
<Variable1>0</Variable1>
<Variable2>0.000000000000000000e+00</Variable2>
<Variable3>0.000000000000000000e+00</Variable3>
<Variable4>0.000000000000000000e+00</Variable4>
</XSec>
<XSec ID="1" name="sec1">
<XSec ID="2" name="sec2">
I have to access specific variables, for example "Variable2". The amount of "XSec" sections can vary in different XML files. I managed to extract the information inside each "XSec", using
import javax.xml.xpath.*
doc = xmlread(xChange_File);
factory = XPathFactory.newInstance;
xpath = factory.newXPath;
temp_var = getXmlValue('Geometry_AC/fuselages/fuselage/XSections/XSec','nodeset',doc,xpath);
for i=0:temp_var.getLength-1
temp_struct(i+1).values = temp_var.item(i).getTextContent;
end
with "getXmlValue" defining the handling of different types. For "nodeset" the code is as follows:
import javax.xml.xpath.*;
if(strcmp(type,'nodeset'))
expression_value = xpath.compile(path);
value = expression_value.evaluate(doc, XPathConstants.NODESET);
end
This provides the following output:
val =
0
0.000000000000000000e+00
0.000000000000000000e+00
0.000000000000000000e+00
My problem is, that this extracts the information of all child nodes of each "XSec" as a single variable containing all information as a continious string. Cutting the string might work, but I feel like there has to be a more elegant solution to get specific values of the child node extracted.
  2 件のコメント
Kevin Gurney
Kevin Gurney 2023 年 3 月 17 日
Would using readstruct help with your workflow?
Jan Knop
Jan Knop 2023 年 3 月 17 日
Yes, it does exactly what I need. Thank you very much :)

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeStructured Data and XML Documents についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by