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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how populate textbox from xml 1

Status
Not open for further replies.

ozane

Technical User
Feb 3, 2005
57
TR
I am receving xml file from a server via javascript. i can populate all xml file to a textbox. but,

is it possible to get the values from xml tags to populate the textboxes with that values?? if yes how

for example i have an element in xml file as "address". i want to get the value of that element and fill the textbox

Thanx

var xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
var xmldoc = new ActiveXObject("Msxml.DOMDocument");
function start(){

xmlHttp.open('GET', server3, true);
xmlHttp.onreadystatechange = event;
xmlHttp.send();
xmldoc=xmlHttp.responseXML;

var dsRoot=xmldoc.documentElement
}
function event(){
if(xmlHttp.readyState == 4){

document.form1.TextBox1.innerText=xmlHttp.responseText
} else {

}
}
 
Hi Ozane,

for future posts, could you surround code examples with [tt][ignore]
Code:
[/ignore][/tt]
tags to improve readability?

Also, a sample of your XML would be helpful.

Thread216-1164456 includes code which extracts a particular field - here's an example of how it would work:


Code:
url="[URL unfurl="true"]http://test.example.com/test.xml";[/URL]

function start() {
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
        xmlhttp.overrideMimeType("text/xml");
    }else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = event;

    xmlhttp.open("GET",url,true);
    xmlhttp.setRequestHeader('content-type', 'text/xml');         
    xmlhttp.send(null);    
}



function event() {
	[COLOR=red]var node = 'address';[/color]
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            var xmlDoc = xmlhttp.responseXML;

            for(n=0; n<xmlDoc.getElementsByTagName(node).length; n++){
                var xmlData = xmlDoc.getElementsByTagName(node)[n].firstChild.data;
            }
        }else{
            alert("There was a problem retrieving the XML data:\n" + xmlhttp.statusText);
        }
    }
}

This example shows how to extract a particular node from an XML file (variable xmlData). You can choose what/where you want to do stuff with this.

Do take a look at my start() function as well - it caters for non-Microsoft browsers such as Firefox.

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
Thanks for help manarth... but couldnt achieve extracting value from xml tag. sorry i am not an expert.... here is the part of my xml file...

Code:
<?xml version="1.0"?>

<csw:Capabilities version="2.0.0"
		xmlns:ogc="[URL unfurl="true"]http://www.opengis.net/ogc"[/URL]
		xmlns:xlink="[URL unfurl="true"]http://www.w3.org/1999/xlink"[/URL]
		xmlns:csw="[URL unfurl="true"]http://www.opengis.net/cat/csw"[/URL]
		xmlns:ows="[URL unfurl="true"]http://www.opengis.net/ows"[/URL]
		xmlns:i18n="[URL unfurl="true"]http://www.esri.com/metadata/i18n/#">[/URL]

	<ows:ServiceIdentification>
		<ows:ServiceType>CSW</ows:ServiceType>
		<ows:ServiceTypeVersion>2.0.0</ows:ServiceTypeVersion>
		<ows:Title>CSW 2.0 Connector for ArcIMS 9.1</ows:Title>
		<ows:Abstract>
			<i18n:string key="SERVICE_ABSTRACT"/>
		</ows:Abstract>
		<ows:Keywords>
			<ows:Keyword>Metadata Catalogue</ows:Keyword>
			<ows:Keyword>keyword 2</ows:Keyword>
		</ows:Keywords>
		<ows:Fees>unknown</ows:Fees>
		<ows:AccessConstraints>Free</ows:AccessConstraints>
	</ows:ServiceIdentification>
 
Manarth,
address is one element of xml. i will need to extract all elements from xml. address was one example. for example i need to get the value of "ServiceType" and others as well

thanks
 
Code:
window.onload = start;

url="[URL unfurl="true"]http://test.example.com/test.xml";[/URL]

function start() {
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
        xmlhttp.overrideMimeType("text/xml");
    }else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = event;

    xmlhttp.open("GET",url,true);
    xmlhttp.setRequestHeader('content-type', 'text/xml');         
    xmlhttp.send(null);    
}



function event() {
    var node = 'ServiceTypeVersion';
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            //alert("opened xml file");
            var xmlDoc = xmlhttp.responseXML;
            parseChildren(xmlDoc.firstChild);
        } else {
            alert("There was a problem retrieving the XML data:\n" + xmlhttp.statusText);
        }
    }
}

function parseChildren(xmlNode) {
	extractData(xmlNode);
    for (var i=0; i<xmlNode.childNodes.length; i++){
		if (xmlNode.childNodes[i].nodeType == 1) parseChildren(xmlNode.childNodes[i]);
		// only descend down tag trees: NOT attributes or text values.
	}
}

function extractData(xmlNode) {
	nodeName = xmlNode.nodeName;
	attributes = xmlNode.attributes;
	textData = (xmlNode.childNodes.length>0) ? xmlNode.firstChild.nodeValue:"";
	numberOfChildren = xmlNode.childNodes.length;
	
	alert(nodeName + " - " + textData + " - " + numberOfChildren + " child nodes");
	
}

This sample will descend the entire tree of the document. The extractData function simply popsup an alert to the node's data - you can replace this with functions to extract the data you need and copy it to an input box.

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
This is in essense how the op should do it.
[tt]
/*[green]
sncname in the role of "Address", say
snsname must match ows's namespace name with ows as prefix in xml
the prefix snsprefix appeared in script is quite arbitrary, not necessary to match the same prefix as appear in xml
[/green]*/

var sncname="Title";
var snsname="var snsprefix="spns";

xmldoc.setProperty ("SelectionNamespaces","xmlns:"+snsprefix+"='"+sncname+"'");
xmldoc.setProperty ("SelectionLanguage","XPath");

//this is the data for further manipulation
var sdata = xmldoc.documentElement.selectSingleNode("//"+snsprefix+":"+sncname).text;
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top