bestimmte XML nodes auslesen

1manni1

Erfahrenes Mitglied
Also ich habe eine unregelmäsige XML die ich auslesen will.
D.h. zB hier

Nun will ich in dieser Xml zB alle childnotes mit dem nodename <GalleryURL> auslesen.
Aber ich weiß nicht genau wie ich das anstellen soll das er mir alle nodes mit galleryurl in ein array einließt denn so wie ich es bis jetzt gemacht habe... also mit dem standort also der Reihenfolge geht das nicht mehr da manchmal ein unwichtiges childnode nicht angezeigt wird
 
Hi,

durchsuch das XML-Objekt einfach rekursiv:
Code:
var galurl = new Array(); // für Deine gesuchten Nodes

var xm = new XML();

xm.ignoreWhite = true;

xm.onLoad = function() {
	parse(this);
	trace(galurl);
}

xm.load("http://open.api.ebay.com/shopping?callname=FindItems&responseencoding=XML&appid=e-sellsG-15b5-4687-8fbb-3797e07f4157&siteid=77&version=517&sellerid(0)=salestrend&MaxEntries=10");

function parse(obj) {
	for (var i=0; i<obj.childNodes.length; i++) {
		var node = obj.childNodes[i];
		if (node.nodeName == "GalleryURL") galurl.push(node.firstChild.nodeValue);
		if (node.childNodes.length) parse(node);
	}
}

Gruß
.
 
ahh vielen Dank genau das habe ich gesucht :) jetzt hab ich das mal weitergeführt aber bin wieder einmal hängen geblieben.

Code:
var sellerid = "e-sells_de";
var totalitems = "2";
var bidcountvar = "";
photo_thumbnail = new Array();
endtime = new Array();
viewitem = new Array();
currentprice = new Array();
currencyid = new Array();
titlename = new Array();
listingtype = new Array();
bidcount = new Array();

var xm = new XML();
xm.ignoreWhite = true;
xm.onLoad = function() {
	parse(this);
	trace(listingtype);
	trace(bidcount);
}

function parse(obj) {
	for (var i=0; i<obj.childNodes.length; i++) {
		var node = obj.childNodes[i];
		if (node.nodeName == "GalleryURL") photo_thumbnail.push(node.firstChild.nodeValue);
		if (node.nodeName == "EndTime") endtime.push(node.firstChild.nodeValue);
		if (node.nodeName == "ConvertedCurrentPrice") currentprice.push(node.firstChild.nodeValue);
		if (node.nodeName == "ConvertedCurrentPrice") currencyid.push(node.attributes.currencyID);
		if (node.nodeName == "Title") titlename.push(node.firstChild.nodeValue);
		if (node.nodeName == "ViewItemURLForNaturalSearch") viewitem.push(node.firstChild.nodeValue);
		if (node.nodeName == "BidCount") bidcountvar = node.firstChild.nodeValue;
		if (node.nodeName == "ListingType"){			
			listingtype.push(node.firstChild.nodeValue);
			if(node.firstChild.nodeValue == "Chinese"){
				bidcount.push(bidcountvar);
			}else if(node.firstChild.nodeValue == "FixedPriceItem"){
				bidcount.push("FixedPriceItem");
			}
		}
		if (node.childNodes.length) parse(node);
	}
}
xm.load("http://open.api.ebay.com/shopping?callname=FindItems&responseencoding=XML&appid=e-sellsG-15b5-4687-8fbb-3797e07f4157&siteid=77&version=517&sellerid(0)=" + sellerid + "&MaxEntries=" + totalitems);

irgendwie wird bei dem Array Bidcount immer am Anfang "" reingesetzt. Wie könnte ich dieses Problem lösen?
 
Hi,

da "bidcountvar" nur gefüllt wird, wenn der Ausdruck
Code:
if (node.nodeName == "BidCount")
war ist, Du diese Variable aber immer ins Array schiebst, wenn jene Ausdrücke war sind:
Code:
if (node.nodeName == "ListingType"){			
			listingtype.push(node.firstChild.nodeValue);
			if(node.firstChild.nodeValue == "Chinese"){
, solltest Du die XML-Datei nach Fällen überprüfen, in denen die erste Bedingung nicht erfüllt ist, die zweiten dagegeb schon.

... oder Du prüfst vor der push-Aktion noch einmal, ob der nodeName des Knotens "node" tatsächlich "BidCount" lautet.

Gruß
.
 
So hatte ich es davor gemacht.

PHP:
		if (node.nodeName == "ListingType"){			
			listingtype.push(node.firstChild.nodeValue);
			if(node.firstChild.nodeValue == "Chinese"){
				if (node.nodeName == "BidCount") bidcount.push(node.firstChild.nodeValue);
			}else if(node.firstChild.nodeValue == "FixedPriceItem"){
				bidcount.push("FixedPriceItem");
			}
		}

Ging allerdings nicht da node.nodeName bei diser if Abfrage immer "ListingType" ist :

PHP:
			if(node.firstChild.nodeValue == "Chinese"){
				if (node.nodeName == "BidCount") bidcount.push(node.firstChild.nodeValue);
			}

Die erste Lösung von die habe ich nicht verstanden...was genau sollte ich prüfen ob es nicht stimmt?
 
Hi,

"bidcountvar" wird nur gesetzt, wenn der aktuelle nodeName "BidCount" lautet.

Du fügst die Variable "bidcountvar" allerdings immer dem Array "bidcount" hinzu, wenn der nodeName "ListingType" lautet.

Beim ersten Mal (wenn vor "ListingType" kein Knoten namens "BidCount" kam), wird daher ein leerer String dem Array hinzugefügt.

Vielleicht genügt es schon, wenn Du folgendes:
Code:
bidcount.push(bidcountvar);
durch jenes ersetzt:
Code:
if (bidcountvar) bidcount.push(bidcountvar);

Gruß
.
 
Klingt logisch also so denke ich mal:

PHP:
		if (node.nodeName == "BidCount") bidcountvar = node.firstChild.nodeValue;
		if (node.nodeName == "ListingType"){			
			listingtype.push(node.firstChild.nodeValue);
			if(node.firstChild.nodeValue == "Chinese"){
				if (bidcountvar) bidcount.push(bidcountvar);
			}else if(node.firstChild.nodeValue == "FixedPriceItem"){
				bidcount.push("FixedPriceItem");
			}
		}

geht allerdings auch noch nicht ganz da beim erstenmal nichts eingetargenwird ^^
 
Hi,

erklär mir doch einfach, was Du auf welche Weise aus dem XML-Objekt extrahieren willst - aus Deinem Ansatz werde ich nämlich nicht schlau. ;)

Gruß
.
 
Also als erstes will ich das ListingType auslesen was ja kein Problem ist. Dann sollte eben bei jedem Ausgelesenen geprüft werden ob es FixedPriceItem ist oder Chinese.

Nun sollte wenn es FixedPriceItem ist in das Array Bidcount "FixedPriceItem" und wenn es "chinese" ist sollte in dem selben Kontenpunkt <Item> in XML der Kontenpunkt <BidCount> ausgelesen werden und ins Array bidcount geschrieben werden.
 

Neue Beiträge

Zurück