Question

I have the following jQuery code that is in a function.

var msgXML = "<XMLInput><Source></Source><MessageText></MessageText><SendTime></SendTime><Destination></Destination><NotUsed></NotUsed></XMLInput>",
msgXMLDoc = $.parseXML(msgXML),
$msgXML = $( msgXMLDoc ),
$msgSource = $msgXML.find("Source"),
$msgText = $msgXML.find("MessageText"),
$msgSTime = $msgXML.find("SendTime"),
$msgDest = $msgXML.find("Destination");

In this function, I need to make some changes to each of the nodes in the XML, and when complete, pass the changed XML into an ajax call that that will take the XML as an input to a SQL stored procedure.

In the code below, I can append the XML values

$msgXML.children(0).append($msgSource.text(mySource));
$msgXML.children(0).append($msgText.text(myMsg));
$msgXML.children(0).append($msgSTime.text(currTimeString));
$msgXML.children(0).append($msgDest.text(myDest));

but it changes the XML structure (I do not set the field <NotUsed></NotUsed> but must pass it) and shuffles around the XML. This is my first issue/question.

The desired structure is:

<XMLInput>
<Source></Source>
<MessageText></MessageText>
<SendTime></SendTime>
<Destination></Destination>
<NotUsed></NotUsed>
</XMLInput>

Using the append above appears to change the values of the node if I put it in the console log:

console.log("Out = " + $msgXML.find("Source").text());

However, the data that's being passed isn't what gets written to the SQL table. This is the second issue. In the console log, it's showing as either undefined or [object Object]. I can display the (changed) text, but I'm obviously not referencing the xml structure properly. What am I doing wrong with each of these?

Was it helpful?

Solution

use

$msgXML.find("Source").append(mySource);

instead of

$msgXML.children(0).append($msgSource.text(mySource));

(similarly for the other parts). $msgXML.children(0) seems to refer to the XMLInput element which would be logical as you start with the xml document node.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top