5

I am trying to modify a status flag in an XML structure using Javascript. Using examples found on the internet I believe this should work:

test = "<?xml version='1.0' encoding='utf-8' standalone='no' ?>" + 
    "<resultaat>" +
        "<type>6</type>" +
        "<status>I</status>" +
        "<start_datum>2012-06-16 00:00:00</start_datum>" +
        "<eind_datum></eind_datum>" +
    "</resultaat>"

To change the content of the status field:

$(test).find("status").text("D")

The result is however that test is not modified and still contains the old status I

Thanks for the answers

The correct insight is that you need to convert to an XMLObject first and modify this.

Below is how I ended up doing it:

/* Convert Text to XML Object */
doc = $.parseXML(test)

/* Change the fields required */
$(doc).find('status').text('D')

/* Back to Text */
str = (new XMLSerializer()).serializeToString(doc);
1
  • 3
    That's because test is a string and you're not modifying it. Commented Jun 29, 2012 at 8:26

3 Answers 3

6

Use jQuery.parseXML()

<p id="someElement"></p>
<p id="anotherElement"></p>

var xml = "<rss version='2.0'><channel><title>RSS Title</title></channel></rss>",
    xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc ),
    $title = $xml.find( "title" );

/* append "RSS Title" to #someElement */
$( "#someElement" ).append( $title.text() );

/* change the title to "XML Title" */
$title.text( "XML Title" );

/* append "XML Title" to #anotherElement */
$( "#anotherElement" ).append( $title.text() );
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the comments, I ended up doing it like this: /* Convert to XML Object / doc = $.parseXML(test) / Modify Content / $(doc).find('status').text('D') / Back to Text */ str = (new XMLSerializer()).serializeToString(doc);
0

you need to write code something like this...

test = "<?xml version='1.0' encoding='utf-8' standalone='no' ?>" + 
    "<resultaat>" +
        "<type>6</type>" +
        "<status>I</status>" +
        "<start_datum>2012-06-16 00:00:00</start_datum>" +
        "<eind_datum></eind_datum>" +
    "</resultaat>";

def = $(test).find("status").text("D");

console.log(def);

To make it work....

3 Comments

That just gives you <status>.
if you carefully see the status variable in console it have <status> as a "D" so its actually changed.
That's not the point; the OP wants the entire XML back, not just the one node.
0

Mmmmm, this answers works... but not allways. I'm using an old webkit version wich is bundled inside Tidesdk and i have some weird problems:

$(xml).find("whatever").append("<however></however>");
// doesn't modify xml

$("<however></however>").appendTo($(xml).find("whatever"));
// does modify xml

??? :_)

Comments

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.