Accessing XML node information

10 views (last 30 days)
Jan Knop
Jan Knop on 17 Mar 2023
Commented: Jan Knop on 17 Mar 2023
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 Comments
Kevin Gurney
Kevin Gurney on 17 Mar 2023
Would using readstruct help with your workflow?
Jan Knop
Jan Knop on 17 Mar 2023
Yes, it does exactly what I need. Thank you very much :)

Sign in to comment.

Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!