Link to home
Start Free TrialLog in
Avatar of pamela rizk
pamela rizkFlag for Lebanon

asked on

Remove node from xml javascript

hi

how can i remove a node from html collection xml in javascript
Avatar of ste5an
ste5an
Flag of Germany image

Please provide a concise and complete example, preferably as stand-alone HTML file.

Please also rephrase your question: "html collection xml"
Is it HTML or XML?
you need to lookup for a path to that node, once you got this path/criteria in your mind, you can create a "query" to point that node and remove it from the xml document
Avatar of pamela rizk

ASKER

please send me the code to make it clear for me
thank you
for more details
i have a xml file
var m_XML_segments_Grid =  ($.parseXML(m_result_segments_Grid); where m_result_segments_Grid is an string
and m_XML_segments_Grid_str = new XMLSerializer().serializeToString(m_XML_segments_Grid);
contain the below :
"<NewDataSet>↵  <DsResQuery>↵    <SEQ>2</SEQ>↵    <SEGMENTNBRE>Segment #1</SEGMENTNBRE>↵    <P_SEGID>1</P_SEGID>↵    <CBOIN>08:00</CBOIN>↵    <CBOOUT>13:00</CBOOUT>↵    <DATE_TIME_STR>09/02/2019</DATE_TIME_STR>↵    <P_DESC>Segment 1 desc</P_DESC>↵    <P_DSCHEMA>8</P_DSCHEMA>↵    <P_WRKSTAT>0</P_WRKSTAT>↵    <P_PRESENT>1</P_PRESENT>↵    <P_STATUS_DAY>0</P_STATUS_DAY>↵    <P_FLOAT>0</P_FLOAT>↵  </DsResQuery>↵  <DsResQuery>↵    <SEQ>3</SEQ>↵    <SEGMENTNBRE>Segment #2</SEGMENTNBRE>↵    <P_SEGID>2</P_SEGID>↵    <CBOIN>14:00</CBOIN>↵    <CBOOUT>17:30</CBOOUT>↵    <DATE_TIME_STR>09/02/2019</DATE_TIME_STR>↵    <P_DESC>Segment 2 desc</P_DESC>↵    <P_DSCHEMA>8</P_DSCHEMA>↵    <P_WRKSTAT>0</P_WRKSTAT>↵    <P_PRESENT>1</P_PRESENT>↵    <P_STATUS_DAY>0</P_STATUS_DAY>↵    <P_FLOAT>0</P_FLOAT>↵  </DsResQuery>↵</NewDataSet>"

i need to add row to this xml
and delete row from this xml where SEQ = 1
i need to update the row where SEQ = 1 with new information
how to do that ?
what code? I believe the issue is in the request itself :
without knowing exactly where's the node to remove or from what criteria you remove the node, you CAN'T do it, it just random code.
I need to add row to this xml
i supposed you mean a <DsResQuery> node

and delete row from this xml where SEQ = 1
so delete a <DsResQuery>  having a children SEQ = 1

I need to update the row where SEQ = 1 with new information
after deleting, no need to update, you insert, before, it's an update

how to do that ?
please confirm above
yes you are right
this is exactly what i want
I need to add row to this xml
// new row
var newDsResQuery = "<DsResQuery>↵    <SEQ>2</SEQ>↵    <SEGMENTNBRE>Segment #1</SEGMENTNBRE>↵    <P_SEGID>1</P_SEGID>↵    <CBOIN>08:00</CBOIN>↵    <CBOOUT>13:00</CBOOUT>↵    <DATE_TIME_STR>09/02/2019</DATE_TIME_STR>↵    <P_DESC>Segment 1 desc</P_DESC>↵    <P_DSCHEMA>8</P_DSCHEMA>↵    <P_WRKSTAT>0</P_WRKSTAT>↵    <P_PRESENT>1</P_PRESENT>↵    <P_STATUS_DAY>0</P_STATUS_DAY>↵    <P_FLOAT>0</P_FLOAT>↵  </DsResQuery>↵";
// insert now
$("NewDataSet", m_XML_segments_Grid).append(newDsResQuery);

Open in new window


and delete row from this xml where SEQ = 1
var SEQ_value = "1";
$("NewDataSet DsResQuery SEQ", m_XML_segments_Grid).filter(function() { return $(this).text()==SEQ_value; }).remove();

Open in new window


I need to update the row where SEQ = 1 with new information
var CBOIN_new_hour = "11:09";
var SEQ_value = "1";
$("NewDataSet DsResQuery SEQ", m_XML_segments_Grid).filter(function() { return $(this).text()==SEQ_value; }).parent().find("CBOIN").text($("NewDataSet DsResQuery SEQ", m_XML_segments_Grid).filter(function() { return $(this).text()==SEQ_value; }));

Open in new window

ok i will test if they work bu i also need to know how to loop to all records with seq = 1 and read each column in the xml?
I need to update the row where SEQ = 1 with new information in the solution you sent :  CBOIN_new_hour  is not used
so how you are updating cboin value with teh new value ?

Kindly advise

var CBOIN_new_hour = "11:09";
var SEQ_value = "1";
$("NewDataSet DsResQuery SEQ", m_XML_segments_Grid).filter(function() { return $(this).text()==SEQ_value; }).parent().find("CBOIN").text($("NewDataSet DsResQuery SEQ", m_XML_segments_Grid).filter(function() { return $(this).text()==SEQ_value; }));

Open in new window


also what if i need to  updtae cboin with new vaue for records in xml that has seq = 0 and p_segid = 1 how to do that?
any help?
here a test page : https://jsfiddle.net/mqu24x87/

var SEQ_value = "2";
$("NewDataSet DsResQuery SEQ", m_XML_segments_Grid).filter(function() { return $(this).text()==SEQ_value; }).each(function(i,v) {
         var DsResQuery = $(this).parent();
         var CBOIN = $("CBOIN", DsResQuery).text();
         var SEGMENTNBRE = $("SEGMENTNBRE", DsResQuery).text();
         alert("CBOIN text is " + CBOIN + "\nSEGMENTNBRE text is " + SEGMENTNBRE);
});

Open in new window

also what if i need to  updtae cboin with new vaue for records in xml that has seq = 0 and p_segid = 1 how to do that?
on the other hand remove is not woking for me(i want to  loop for all records in xml that has seq = 2 and delete the related node  how to do that ?
var SEQ_value = "2";
$("NewDataSet DsResQuery SEQ", m_XML_segments_Grid).filter(function() { return $(this).text()==SEQ_value; }).each(function(i,v) {
         var DsResQuery = $(this).parent();
         var CBOIN = $("CBOIN", DsResQuery).text("mefkemdlkmldkmdgkdk");  // need to  updtae cboin with new vaue
         $(this).parent().remove(); // remove DsResQuery where SEQ = 2
         DsResQuery.remove(); // remove DsResQuery where SEQ = 2
});

Open in new window

ok delete is working and update is working for a specific where condition (seq = 2)
what if i want to updatecboin with new value for records in xml that has seq = 2 and p_segid = 1 how to do that?
if you initial question is answered, please open a new thread
No, my main question was  conserning insert /update and delete
delete and insert are well answered i  till have the update problem
(what if i want to updatecboin with new value for records in xml that has seq = 2 and p_segid = 1 how to do that?)
Please help me in order to close this question
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you mr leakim971

you solve my entire probem for how to insert/delete/update data in an XMl in javascript.
thank you