how to replace a child node with another child node in an XML file?
Hi,
I have an XML snippet in the following format.
</Markings>
<Profiles>
<Profile name="LAKECITY_2x2voies+tp+park+trottoir" type="none">
<LaneBorder distance="-15.1 " height="0.15 " markingOffset="0 "/>
<LaneBorder distance="-12.15 " height="0.15 " markingOffset="0 "/>
<LaneBorder distance="-12.1 " height="0 " markingOffset="0.15 "/>
<LaneBorder distance="-11.8 " height="0 " markingOffset="0 "/>
<Lane Ground="Cobblestone " circulationWay="both " name="Lane " speedLimit="8.33333 " type="sidewalk "/>
<Lane Ground="Asphalt " circulationWay="none " name="Lane 2 " speedLimit="0 " type="shoulder "/>
<Lane Ground="Asphalt " circulationWay="none " name="Lane 3 " speedLimit="0 " type="shoulder "/>
<Lane Ground="Asphalt " circulationWay="inverse " name="Lane 4 " speedLimit="13.8888888888889 " type="parking "/>
<Lane Ground="Asphalt " circulationWay="none " name="Lane 3 " speedLimit="0 " type="shoulder "/> </Profile>
</Profiles>
</RoadNetwork>As can be seen from the snippet, the arrangement of the nodes is such that all the LaneBorder nodes are listed at first and then the Lane nodes. I am trying to change this format in such a way that the LaneBorder nodes and Lane nodes alternate thelselves like shown in the snippet below:
<Profiles>
<Profile name="LAKECITY_2x2voies+tp+park+trottoir" type="none">
<LaneBorder distance="-15.1 " height="0.15 " markingOffset="0 "/>
<Lane Ground="Cobblestone " circulationWay="both " name="Lane " speedLimit="8.33333 " type="sidewalk "/>
<LaneBorder distance="-12.15 " height="0.15 " markingOffset="0 "/>
<Lane Ground="Asphalt " circulationWay="none " name="Lane 2 " speedLimit="0 " type="shoulder "/>
<LaneBorder distance="-12.1 " height="0 " markingOffset="0.15 "/>
<Lane Ground="Asphalt " circulationWay="none " name="Lane 3 " speedLimit="0 " type="shoulder "/>
<LaneBorder distance="-11.8 " height="0 " markingOffset="0 "/>
<Lane Ground="Asphalt " circulationWay="inverse " name="Lane 4 " speedLimit="13.8888888888889 " type="parking "/>
</Profile>
</Profiles>I tried the following steps to achieve this:
profilesNode = docNode.getElementsByTagName('Profiles'); %Get all the 'Profiles' in the document
prof1 = profileNodeCol.item(0); %Getting the first 'Profile' node
LaneCollection = prof1.getElementsByTagName('Lane'); %Getting all the 'Lane' nodes inside 'Profile'
LBcollection = prof1.getElementsByTagName('LaneBorder'); %Getting all the 'LaneBorder' nodes inside 'Profile'
prof1ChildNodes = prof1.getChildNodes; %Getting all the childNodes inside 'Profile'
prof1ChildNodes.replaceChild(LaneCollection.item(0),prof1ChildNodes.item(1)) %Trying to replace the second childNode of Profile with first 'Lane' nodeWhen i tried this, all that happened was that the count inside "prof1ChildNodes" kept on reducing. It was 16 at first and then it kept on reducing with the number of replacements that i tried.
Is there any way in which i can acheive this functionality? Any assistance would be of great help!!!
Thanks in advance,
Girish
回答 (1 件)
カテゴリ
ヘルプ センター および File Exchange で Introduction to Installation and Licensing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!