Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding child element to xml file XMLDOM :S 1

Status
Not open for further replies.

werD420

Technical User
Sep 14, 2004
181
US
Hello all im trying to take a returned xml file that the body looks like this
Code:
 <string xmlns="[URL unfurl="true"]http://microsoft.com/webservices/">Entry[/URL] 1<br/> Entry2<br/>
</string>

Im trying to split this data using javascript and then append the data as individual Child Elements of String. Im able to create an array using .split and i get no errors on my update statements but when i alert the text it still gives me the <br/>'s heres my function any help is greatly appreciated

Code:
function placeHint3(xmlresponse){
var xmlDom = new ActiveXObject("Microsoft.XMLDOM")
xmlDom.async = false
xmlDom.setProperty("SelectionNamespaces", "xmlns:ph='[URL unfurl="true"]http://microsoft.com/webservices/'");[/URL]
xmlDom.setProperty("SelectionLanguage", "XPath");
xmlDom.load(xmlresponse)
var xmlSplit= xmlDom.text.split('<br/>')

//This runs through w/o error but i have a feeling
// its never happening
//anyone know a better way to do this and erase
//the current text with a blank
for (var i=0; i < xmlSplit.Length; i++){
    var CreateElement = xmlDom.createElement('ph:client'+i)
       CreateElement.setValue(xmlSplit(i));
       CreateElement.append
      }
  //SelectNode = xmlDom.selectSingleNode('client2')  
alert(xmlDom.text)    
traverse(xmlDom)

}

Thanks in advance

MCP, .Net Solutions Development <%_%>
 
Just want to note that your use of split is not well conceived. (.text won't pick br tag up in any case.) Also setValue and append are incorrect. Could you check documentation first?
 
This is the bare minimal way.
[tt]
function placeHint3(xmlresponse){
var xmlDom = new ActiveXObject("Microsoft.XMLDOM")
xmlDom.async = false
[green]//not critically needed[/green]
[red]//[/red]xmlDom.setProperty("SelectionNamespaces", "xmlns:ph='[red]//[/red]xmlDom.setProperty("SelectionLanguage", "XPath");
xmlDom.load(xmlresponse)

var oroot=xmlDom.documentElement;
var celem=new Array();
for (var i=0;i<oroot.childNodes.length;i++) {
var ochild=ostring.childNodes;
if (ochild.nodeType==3) {
var oelem=xmlDom.createNode(1,'client'+i," oelem.text=ochild.nodeValue.replace(/^ *| *$/,"");
celem.push(oelem);
}
}
for (var i=oroot.childNodes.length-1;i>-1;i--) {
oroot.removeChild(oroot.childNodes);
}
for (var i=0;i<celem.length;i++) {
oroot.appendChild(celem);
}
traverse(xmlDom) [green]//whatever it is[/green]
}
[/tt]
 
Amendment
This is due to a change of mind during draft up. The correct reading of the corresponding line is this.
[tt] var ochild=o[red]root[/red].childNodes;
[/tt]
 
Thanks tsuji for your response. I am trying my damndest to read all the documentation i can but everything i find tends to be really vague ie. just showing method names and properties but not many real world examples.

The function you provided gives me the same result as before everything is passed as one node <string> if i alert oroot.text it gives me the same statement as xmlDom.text did
ie.
Entry 1<br/> Entry2<br/>

Whenever i use split function im able to alert them as Entry 1,Entry 2... and im able to call them by number ie
xmlSplit(i) and it calls the correct one?

is there an update method or something im missing or do i need to reload the doc or what? Im sorry if I sound clueless, I swear ive been reading every single piece of documentation i can get my hands on

Thanks
DrewG


MCP, .Net Solutions Development <%_%>
 
I see whats going on when i alert oroot.xml
it gives me
<string>
<client0>entry 1 &lt;br/&gt; entry 2</client0>
</string>

My desired output is
<string>
<client0>
entry 1
</client0>
<client1>
entry 2
</client1>
</string>


I appreciate your code this seems to be what i needed though to get me going

Thanks

MCP, .Net Solutions Development <%_%>
 
Im sorry to ask again but the added arrays has me a bit confused
Code:
for (var i=0;i<oroot.childNodes.length;i++) {
This cant work in this case because there is no child node to begin with, Is there a way to use a loop similar to the one in my first post to do this? Also Isnt split neccessary to achieve this?

Thanks again

MCP, .Net Solutions Development <%_%>
 
I don't see consistency of your feedback.

[1] What is xmlresponse? After the line xmlDom.load(xmlresponse), alert the content
[tt]alert(xmlDom.xml);[/tt]
If that is okay, you can go on. If not, you better make sure the parsing take place flawless. Without this solid, the rest is empty talk.

[2] This demo is to establish the correct framework to work on the algorithm I show you. The demo load a string, so I change the line from load to loadXML.
[tt]
function placeHint3_text(xmlresponse_text){
var xmlDom = new ActiveXObject("Microsoft.XMLDOM")
xmlDom.async = false
//not critically needed
//xmlDom.setProperty("SelectionNamespaces", "xmlns:ph='//xmlDom.setProperty("SelectionLanguage", "XPath");
[blue]
//xmlDom.load(xmlresponse)
xmlDom.loadXML(xmlresponse_text);
[/blue]
var oroot=xmlDom.documentElement;
var celem=new Array();
for (var i=0;i<oroot.childNodes.length;i++) {
var ochild=oroot.childNodes;
if (ochild.nodeType==3) {
var oelem=xmlDom.createNode(1,'client'+i," oelem.text=ochild.nodeValue.replace(/^ *| *$/,"");
celem.push(oelem);
}
}
for (var i=oroot.childNodes.length-1;i>-1;i--) {
oroot.removeChild(oroot.childNodes);
}
for (var i=0;i<celem.length;i++) {
oroot.appendChild(celem);
}
[blue]
//traverse(xmlDom) //whatever it is
alert(xmlDom.xml);
[/blue]
}

var s='<string xmlns=" 1<br/> Entry2<br/></string>';
placeHint3_text(s);
[/tt]
[3]
> I am trying my damndest to read all the documentation i can but everything i find tends to be really vague ie. just showing method names and properties but not many real world examples.
Sounds like kid talk. I don't even advocate that I have read _all_ the documentation. Real world examples? It only sounds like meaningful. Is your example real enough? Is "real" built on fictitious? Is air more real than oxygen and nitrogen?
 
[4]
>Also Isnt split neccessary to achieve this?
Do you know what "really" is xmlDom.text is in your line like this?
>var xmlSplit= xmlDom.text.split('<br/>')
There won't be any "<br/>" in that xmlDom.text. Do you know how to check this fact?
 
Thanks tsuji you're code was just the start i needed
here's the method working well for my case
Code:
function placeHint4(xmlresponse){
var xmlDom = new ActiveXObject("Microsoft.XMLDOM")
xmlDom.async = false
xmlDom.load(xmlresponse)
var oroot=xmlDom.documentElement;
var xmlSplit= xmlDom.text.split('<br/>')
var celem=new Array();
for (var i=0;i<xmlSplit.length;i++) {
    var ochild=oroot.childNodes[0];
    if (ochild.nodeType==3) {
        var oelem=xmlDom.createNode(1,'client'+i,"[URL unfurl="true"]http://microsoft.com/webservices/");[/URL]
        oelem.text=xmlSplit[i]
        celem.push(oelem);
    }
}

for (var i=oroot.childNodes.length-1;i>-1;i--) {
    oroot.removeChild(oroot.childNodes[i]);
}
for (var i=0;i<celem.length;i++) {
    oroot.appendChild(celem[i]);
}
traverse(oroot)    
}

Thanks again

MCP, .Net Solutions Development <%_%>
 
man i just saw your other posts im sorry if i upset you.
By "reading all the documentation i can" i meant that i read all that i can find so far im still reading and its still a learning process that will never be complete but i have been searching hard through google.

again thanks for your help and sorry to bother you

MCP, .Net Solutions Development <%_%>
 
i think alot of the misunderstanding was based on "xmlresponse" here is where it originates from

Code:
function postText(){
var url="[URL unfurl="true"]http://localhost:4244/WebSite2/hint.asmx/hint";[/URL]
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
xmlHttp.open('POST', url , true);
xmlHttp.setRequestHeader("Content-Type","application/x-[URL unfurl="true"]www-form-urlencoded");[/URL]
var sendText=document.getElementById("Term").value
xmlHttp.send("Term="+sendText);
xmlHttp.onreadystatechange = checkState;
 }
function checkState(){
  if(xmlHttp.readyState=="complete"||xmlHttp.readyState==4){
  [COLOR=red]placeHint4(xmlHttp.responseXML);[/color]
   }

Thanks

MCP, .Net Solutions Development <%_%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top