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 CenterFile Exchange에서 Structured 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