matlab中的xml节点替换
我有一个以下格式的XML片段matlab中的xml节点替换,xml,matlab,replace,Xml,Matlab,Replace,我有一个以下格式的XML片段 </Markings> <Profiles> <Profile name="LAKECITY_2x2voies+tp+park+trottoir" type="none"> <LaneBorder distance="-15.1 " height="0.15 " markingOffset="0 "/>
</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>
当我尝试这样做时,所发生的一切就是“prof1ChildNodes”中的计数不断减少。起初是16个,然后随着我尝试更换的次数不断减少
有什么方法可以实现这个功能吗?任何帮助都会大有裨益
提前感谢,
Girish我碰巧自己找到了解决办法。显然我唯一要做的就是 a) 获取所需的“Profile”节点对象 b) 将“Lanes”和“LaneBorders”分为两个独立的集合 c) 使用“node.insertBefore(newNode,existingNode)”方法在两个必需的“laneborder”之间插入每个“Lane”,如下面的代码所示
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
谢谢@pkpk的提示
它对循环使用3个嵌套。这是一种否定,但它实现了函数性。如果有人能提出一个更好、最理想的方法,这个建议真的很值得一提
提前谢谢
问候,
Girish尝试使用Thank you for your coment,但是将xml转换为结构对我有什么帮助呢?我想使用结构来更改元素的顺序会容易得多。
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
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