Read specific value from XML-file

Hi guys,
my question is about the reading of information from an xml-file of only a special type of object
Example part of XML-file:
<cim:Car rdf:ID="_00c7c220-2f78-4eb0-80fd">
<cim:Equipment.BaseGroup rdf:resource="#_29355f3f-035d-494e-8549" />
<cim:Motor.weigth>10</cim:Motor.weigth>
<cim:Motor.length>2.36517e01</cim:Motor.length>
<cim:Motor.length>7</cim:Motor.length>
<cim:IdentifiedObject.name>Nice_blue_car</cim:IdentifiedObject.name>
</cim:Car>
<cim:Car rdf:ID="_01281464-7eca-43db-aca9">
<cim:Equipment.BaseGroup rdf:resource="#_29355f3f-035d-494e-8549" />
<cim:Motor.weigth>15</cim:Motor.weigth>
<cim:Motor.length>2.33165e01</cim:Motor.length>
<cim:Motor.length>4</cim:Motor.length>
<cim:IdentifiedObject.name>Nice_red_car</cim:IdentifiedObject.name>
</cim:Car>
<cim:Bike rdf:ID="_02de5fa9-368e-4d5d-9a2f">
<cim:Equipment.BaseGroup rdf:resource="#_29355f3f-035d-494e-8549" />
<cim:Motor.weigth>0.5</cim:Motor.weigth>
<cim:Motor.length>4.54142e01</cim:Motor.length>
<cim:Motor.length>2</cim:Motor.length>
<cim:IdentifiedObject.name>Nice_yellow_bike</cim:IdentifiedObject.name>
</cim:Bike>
In this example, there are vehicles listed. Some cars and a bike. For example, i want to read out the names ("IdentifiedObject.name") but only from all objects out the the group Car. The result should be a cell, with size (number of elements of type car x 1) with the names in.
How can I do this?

답변 (1개)

Fangjun Jiang
Fangjun Jiang 2021년 5월 6일

0 개 추천

The short answer is to use xmlread(). I've done it before. The actual code depends on the schema.
You may also try xml2struct(). It seems much easier.

댓글 수: 3

I am already using xmlread. Later on i am extracting the specific information with two subfunctions
for text information:
xml.item(t).getTextContent()
for ID information:
for t=0:xml.getLength-1
Attr=parseAttributes(xml.item(t));
switch Type
case 'ID'
out{t+1,1}=Attr.rdf_colon_ID;
case 'resource'
out{t+1,1}=Attr.rdf_colon_resource;
case 'about'
out{t+1,1}=Attr.rdf_colon_about;
end
end
There i can only read out all IdentifiedObject.name but I only want to get the names from type car.
XML2struct is not useful, because of the deep structure of the xml file with long names, so that there will be a mess of structs later on.
Fangjun Jiang
Fangjun Jiang 2021년 5월 6일
편집: Fangjun Jiang 2021년 5월 6일
I used the getElementsByTagName() method.
Marc Gebhardt
Marc Gebhardt 2021년 5월 6일
Then let us leave this kind of general answers please.
Just write one command, which lists the names for this easy example withput a loop over all elements.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

질문:

2021년 5월 6일

편집:

2021년 5월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by