Changing values in an existing XML file
조회 수: 25 (최근 30일)
이전 댓글 표시
Hi All,
I am fairly new to using matlab to parse through an XML file. I have an exisiting XML file that I am looking to change certain values. I am able to access the a level above the information that I actually want. I am unable to figure out how to alter the Location of each to the "Force Set" and also read the Body string so that I can do a logical comparision.
When I try to get the Location text content (variable: data) I get an error saying "Dot indexing is not supported for variables of this type."
<ForceSet>
<objects>
<Thelen2003Muscle name="LVS">
<!--Flag indicating whether the force is disabled or not. Disabled means that the force is not active in subsequent dynamics realizations.-->
<isDisabled>false</isDisabled>
<!--The set of points defining the path of the muscle.-->
<GeometryPath>
<!--The set of points defining the path-->
<PathPointSet>
<objects>
<PathPoint name="DELT2-P2">
<location> -0.037263 0.0944378 0.0225438</location>
<body>thorax</body>
</PathPoint>
<PathPoint name="DELT2-P3">
<location> -0.0676471 0.000672336 -0.0956535</location>
<body>scapula</body>
</PathPoint>
</objects>
</PathPointSet>
xmlfile = fullfile('Model.xml');
DOMnode = xmlread(xmlfile);
ForceSet = DOMnode.getElementsByTagName('ForceSet');
Muscle = ForceSet.item(0).getElementsByTagName('Thelen2003Muscle'); % Level that contains all the muscles
no_Musc_idx = Muscle.getLength-1 % Determines the max number of muscles
for i = 0 : no_Musc_idx % Changes what muscle we are looking at (Range : [0,25])
Muscle_name = char(Muscle.item(i).getAttribute('name')) % returns the muscle name 0 - "LVS", 1 - "TRP1"
GeometryPath = Muscle.item(i).getElementsByTagName('GeometryPath');
PathPointSet = GeometryPath.item(0).getElementsByTagName('PathPointSet');
PathPoint = PathPointSet.item(0).getElementsByTagName('PathPoint');
for j =0: PathPoint.getLength -1 % goes through the muscle attachment points
attachmentPoint_name= char(PathPoint.item(j).getAttribute('name')) % The name of the of node its looking at
KidNode = PathPoint.item(j).getTextContent %Prints out both the children nodes...
Location = PathPoint.item(0).getElementsByTagName('Location');
data = Location.item(0).getTextContent %trying to access the location value
end
% class(Location.item(0)) % This lets us know that the answer is a double.... I THINK?!?
% PathPoint.item(j).setTextContent('0 0 0 0') % Sort of works but
% changes the whole node and does not isolate the children nodes
end
댓글 수: 1
Robert Ungi
2022년 1월 11일
This might help https://www.thewizz.art/2022/01/06/how-to-modify-an-xml-attribute-value-using-matlab/
답변 (1개)
Abhishek Gupta
2020년 9월 7일
Hi Natasha,
The reason for getting the error is that there is no element with the tag name 'Location' (it's 'location').
You can make the following changes in your code: -
From
Location = PathPoint.item(0).getElementsByTagName('Location');
To
Location = PathPoint.item(0).getElementsByTagName('location');
Referring to the following link, which might help you in changing values in the XML file: -
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!