필터 지우기
필터 지우기

how to replace a child node with another child node in an XML file?

조회 수: 4 (최근 30일)
Giri
Giri 2018년 5월 31일
편집: Giri 2018년 6월 1일

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' node

When 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개)

Giri
Giri 2018년 6월 1일
편집: Giri 2018년 6월 1일
I happed to find the solution myself on this. Apparently the only thing that i had to do was
a) Get the required "Profile" node object
b) Seperate out the "Lanes" and "LaneBorders" into two seperate collections
c) Insert each "Lane" between the two required "LaneBorders" using the method "node.insertBefore(newNode,existingNode)" as seen in the code below.
profilesNodeCol = docNode.getElementsByTagName('Profiles'); %Getting all the "Profiles" nodes
for k= 0: profilesNodeCol.getLength-1
profilesK = profilesNodeCol.item(k); %Getting the kth profile
for iCount=0:profilesK.getLength
LBcol = profilesK.item(iCount).getElementsByTagName('LaneBorder'); %Creating a LaneBorder collection
laneCol = profilesK.item(iCount).getElementsByTagName('Lane'); %Creating a Lane collection
for j=0: laneCol.getLength-1
try
profilesK.item(iCount).insertBefore(laneCol.item(j),LBcol.item(j+1)); %Inserting the required Lane between the two required LaneBorders
catch ME
error('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
end
end
end
end
It uses a nesting of 3 for loops. Which is a kind of NO-NO but it acheives the funcionality. If anyone could suggest a better and optimal way, the tip would be really apreciated.
thanks in advance.
Regards, Giri

카테고리

Help CenterFile 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!

Translated by