简体   繁体   English

在Matlab中查找和替换XML文件行

[英]Find and replace lines of XML File in Matlab

I have here an XML File, where I have to find the line numbers of a certain tag and then delete everything before that line. 我这里有一个XML文件,在这里我必须找到某个标记的行号,然后删除该行之前的所有内容。

I tried using xml2struct, but that doesn't give me the possibility to go to a certain linenumber and delete everything before that and then write it back into a new XML file. 我尝试使用xml2struct,但这并不能使我转到某个行号并删除之前的所有内容,然后将其写回到新的XML文件中。

Really sorry if this is not worded correctly, I am super new to Matlab and XML file structures... 非常抱歉,如果措辞不正确,我对Matlab和XML文件结构是超级新手...

This is the XML File: ` 这是XML文件:

<?xml version="1.0" encoding="utf-8"?>
<SetBeam>
  <ControlPoints>
    <Cp>
      <Mu>1</Mu>
      <GantryRtn>40.79545455</GantryRtn>
      <Y1>4</Y1>
      <Y2>3.25</Y2>
      <X1>4.45455</X1>
      <X2>3.20455</X2>
      <Mlc>
        <ID>1</ID>
        <B>0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 -4.279 -4.279 1.812 2.395 0.687 3.187 3.219 2.363 3.469 3.469 2.594 1.043 0.25 1.5 3.75 3.75 3.75 3.75 3.75 1.062 -1.531 3.312 3.469 3.219 2.969 2.969 2.719 1.863 1.5 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125</B>
        <A>-0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 4.331 4.331 -1.656 -1.094 0.188 -1.594 -2.062 0.438 -1.582 0.938 -0.301 1.938 1.355 -0.812 2.688 -0.312 -0.5 2.387 -1.906 2.969 2.719 -2.387 -2.5 -2.219 2.469 -0.426 0.355 -0.062 -0.031 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125</A>
      </Mlc>
    </Cp>
    <Cp>
      <Mu>2</Mu>
      <GantryRtn>42.38636364</GantryRtn>
      <Y1>4</Y1>
      <Y2>3.25</Y2>
      <X1>4.45455</X1>
      <X2>3.20455</X2>
      <Mlc>
        <ID>1</ID>
        <B>0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 -4.279 -4.279 1.969 2.719 1.094 3.031 3.219 2.687 3.469 3.719 2.281 1.691 0.25 1.281 3.75 3.75 3.75 3.75 3.75 1.687 -1.156 3.312 3.281 3.219 2.969 2.969 2.719 1.5 1.5 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125</B>
        <A>-0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 4.331 4.331 -1.719 -1.062 0.188 -2.031 -1.719 0.313 -1.125 0.906 0.344 1.938 2.004 -0.75 2.688 -0.062 -0.094 2.688 -1.344 2.906 2.719 -2.125 -2.344 -1.625 2.469 -0.781 0.781 -0.062 -0.031 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125 -0.125</A>
      </Mlc>
    </Cp>
  </ControlPoints>
</SetBeam>

` `

So basically I can navigate through the struct which I created with xml2struct and can extract the values of the nodes, but I couldn't find out, how I can navigate to, say, the node with text '2' and delete the whole node before the node which has the node with text '2'. 因此,基本上,我可以浏览使用xml2struct创建的结构并可以提取节点的值,但是我找不到如何导航到具有文本“ 2”的节点并删除整个节点的方法。在具有带有文本“ 2”的节点的节点之前。 Thanks for any help! 谢谢你的帮助!

Lines aren't generally significant in XML, so thinking of this in terms of lines of the file (rather than the hierarchic XML structure) isn't really the right way to think of the problem, and is therefore going to give you problems because many XML parsers don't report line numbers. 行在XML中通常并不重要,因此从文件的行(而不是分层XML结构)的角度来考虑并不是真正解决问题的正确方法,因此会给您带来问题,因为许多XML解析器不报告行号。

Also, deleting everything before a particular line will almost certainly result in something that isn't well-formed XML. 同样,删除特定行之前的所有内容几乎肯定会导致XML格式不正确。

If you want to delete all the <Cp> elements that precede the one with child <Mu>2</Mu> , you can do this in XSLT easily enough. 如果要删除子元素<Mu>2</Mu>之前的所有<Cp>元素,则可以在XSLT中轻松完成此操作。 You need one rule that copies everything through unchanged, which in XSLT 3.0 can be written 您需要一个规则,该规则将所有内容复制为不变,这可以在XSLT 3.0中编写

<xsl:mode on-no-match="shallow-copy"/>

but in earlier versions needs the more verbose 但在较早的版本中需要更详细

<xsl:template match="*">
  <xsl:copy><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy>
</xsl:template>

and then a second rule that deletes the selected elements: 然后是删除所选元素的第二条规则:

<xsl:template match="Cp[following-sibling::Cp/Mu='2']"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM