0

I am trying to manipulate an xml file sitting on a server utilizing node-elementtree in a nodejs app... I have had no problems writing to the file and saving it on the server but am completely baffled on how to remove an element.

I'm reading in the file and successfully locating the element that I want to remove from the doc before saving:

fs.readFile(coursePath, function(err, data){
    var XML = et.XML;
    var ElementTree = et.ElementTree;
    var element = et.Element;
    var subElement = et.SubElement;

    var _data, etree;

    _data = data.toString();
    etree = et.parse(_data);
    var stringID = content.id.toString();
    console.log(etree.find('./item/[@id="'+stringID+'"]'));
    var myitem = etree.find('./item/[@id="'+stringID+'"]');     

But, when I try to remove it:

etree.remove(myitem);

I get the following error:

[TypeError: undefined is not a function]
TypeError: undefined is not a function
   at /Users/xxxxxxx/Sites/xxxxxx/bin/server/xxxxxxx-socket-handler.js:915:19
   at fs.js:291:14
   at Object.oncomplete (fs.js:97:15)

The docs are kind of sparse and I've been banging my head off of something that I thought would be fairly simple, for hours - any advice would be greatly appreciated.

Best, Phil

1 Answer 1

2

The call should be etree.getroot().remove(myitem);

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the late response... Just saw this - this is correct!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.