Javascript从xml删除节点

时间:2018-12-17 23:17:35

标签: javascript xml

我已经阅读了很多有关此的文章,但无法使其正常工作。这是我的xml

select * from customers c
inner join attributes a
on c.customer_id = a.customer_id
where attributename = 'cash sale'
or attributename = 'email statement'

我需要删除某些trkpt节点(包括属性和属性值(纬度和经度)以及子节点(ele和时间)-完整的trkpt节点。

这是我的代码。整个xml文件称为gpxDoc。需要删除的节点是节点p

<?xml version="1.0" encoding="UTF-8"?>
<gpx>
  <trk>
    <trkseg>
      <trkpt lat="47.66094590" lon="8.857512470">
        <ele>401.00000</ele>
        <time>2010-01-01T00:00:00Z</time>
      </trkpt>
      <trkpt lat="47.66006430" lon="8.858885760">
        <ele>406.00000</ele>
        <time>2010-01-01T00:00:51Z</time>
      </trkpt>
      <trkpt lat="47.65942840" lon="8.859422200">
        <ele>407.00000</ele>
        <time>2010-01-01T00:01:20Z</time>
      </trkpt>
      <trkpt lat="47.65913930" lon="8.859529490">
        <ele>407.00000</ele>
        <time>2010-01-01T00:01:32Z</time>
      </trkpt>
      <trkpt lat="47.65749170" lon="8.858370780">
        <ele>403.00000</ele>
        <time>2010-01-01T00:02:45Z</time>
      </trkpt>
      <trkpt lat="47.65779520" lon="8.857276430">
        <ele>400.00000</ele>
        <time>2010-01-01T00:03:17Z</time>
      </trkpt>
    </trkseg>
  </trk>
</gpx>

但是它不会删除该节点。有什么想法吗?

添加了评论: 我添加了以下控制台日志行

trkseg = gpxDoc.querySelectorAll("trkseg");
trkPoints = trkseg[0].querySelectorAll("trkpt");

for (p = 0; p < trkPoints.length - 2; p++) {
    // Some code to check what if the node needs deleting
    if (offset > offsetLimit) { // needs deleting
        x = gpxDoc.getElementsByTagName("trkpt")[p];
        x.parentNode.removeChild(x);
    }
}

console.log(x)行显示trkpt节点

console.log(trkPoints.length); // length is shown as 149 nodes
var x = gpxDoc.getElementsByTagName("trkpt")[p + 1];
console.log(x);
x.parentNode.removeChild(x);
console.log(trkPoints.length); // length is shown as 149 nodes

0 个答案:

没有答案