Mijn XML-bestandsindeling is zoals hieronder.
<markers>
<marker>
<type></type>
<title></title>
<address></address>
<latitude></latitude>
<longitude></longitude>
<marker>
<marker>
<type></type>
<title></title>
<address></address>
<latitude></latitude>
<longitude></longitude>
<marker>
</markers>
stel me alstublieft voor hoe ik al het “marker” -element kan lezen.
Ik moet de waarde krijgen van alle onderliggende elementen van de “markering”
Bedankt
Antwoord 1, autoriteit 100%
De onderstaande code converteert elk XMLObject of tekenreeks naar een native JavaScript-object. Dan kun je over het object lopen om elke gewenste waarde te extraheren.
/**
* Tries to convert a given XML data to a native JavaScript object by traversing the DOM tree.
* If a string is given, it first tries to create an XMLDomElement from the given string.
*
* @param {XMLDomElement|String} source The XML string or the XMLDomElement prefreably which containts the necessary data for the object.
* @param {Boolean} [includeRoot] Whether the "required" main container node should be a part of the resultant object or not.
* @return {Object} The native JavaScript object which is contructed from the given XML data or false if any error occured.
*/
Object.fromXML = function( source, includeRoot ) {
if( typeof source == 'string' )
{
try
{
if ( window.DOMParser )
source = ( new DOMParser() ).parseFromString( source, "application/xml" );
else if( window.ActiveXObject )
{
var xmlObject = new ActiveXObject( "Microsoft.XMLDOM" );
xmlObject.async = false;
xmlObject.loadXML( source );
source = xmlObject;
xmlObject = undefined;
}
else
throw new Error( "Cannot find an XML parser!" );
}
catch( error )
{
return false;
}
}
var result = {};
if( source.nodeType == 9 )
source = source.firstChild;
if( !includeRoot )
source = source.firstChild;
while( source ) {
if( source.childNodes.length ) {
if( source.tagName in result ) {
if( result[source.tagName].constructor != Array )
result[source.tagName] = [result[source.tagName]];
result[source.tagName].push( Object.fromXML( source ) );
}
else
result[source.tagName] = Object.fromXML( source );
} else if( source.tagName )
result[source.tagName] = source.nodeValue;
else if( !source.nextSibling ) {
if( source.nodeValue.clean() != "" ) {
result = source.nodeValue.clean();
}
}
source = source.nextSibling;
}
return result;
};
String.prototype.clean = function() {
var self = this;
return this.replace(/(\r\n|\n|\r)/gm, "").replace(/^\s+|\s+$/g, "");
}
Antwoord 2, autoriteit 86%
U kunt het onderstaande script gebruiken voor het lezen van de onderliggende XML van de bovenstaande XML. Het werkt beide met IE en Mozila Firefox.
<script type="text/javascript">
function readXml(xmlFile){
var xmlDoc;
if(typeof window.DOMParser != "undefined") {
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET",xmlFile,false);
if (xmlhttp.overrideMimeType){
xmlhttp.overrideMimeType('text/xml');
}
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
}
else{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load(xmlFile);
}
var tagObj=xmlDoc.getElementsByTagName("marker");
var typeValue = tagObj[0].getElementsByTagName("type")[0].childNodes[0].nodeValue;
var titleValue = tagObj[0].getElementsByTagName("title")[0].childNodes[0].nodeValue;
}
</script>
Antwoord 3, Autoriteit 57%
Als u dit van een webserver krijgt, bekijk dan jQuery . U kunt het laden, met behulp van de ajax load-functie en selecteer het knooppunt of de tekst die u wilt, selectors .
Als u dit niet wilt doen in een HTTP-omgeving of vermijd het gebruik van jQuery, leg dan meer in detail uit.
Antwoord 4, Autoriteit 29%
je kunt zoiets doen om je knooppunten te lezen.
Ook kunt u wat uitleg vinden in deze pagina http://www.compoc.com/tuts/
<script type="text/javascript">
var markers = null;
$(document).ready(function () {
$.get("File.xml", {}, function (xml){
$('marker',xml).each(function(i){
markers = $(this);
});
});
});
</script>