JavaScript Wert eines Elementknoten auslesen

  • Ersteller Ersteller Speedy.
  • Erstellt am Erstellt am
S

Speedy.

Gast
Hallo zusammen,

ich bin nicht so bewandert in JavaScript, daher entschuldige ich mich schon jetzt, sollte diese Frage dämlich erscheinen.
Ich will ein XML in JavaScript parsen, was soweit auch wunderbar klappt. Allerdings stoße ich an meine Grenzen, wenn ich den Wert oder Inhalt der Elementknoten aus diesem XML (MESSAGE) anzeigen lassen möchte. Ich wundere mich sowieso, warum die als Elementknoten erkannt werden, aus meiner Sicht sollten das Textknoten sein:

Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<node>
	<item>
		<EXCEPTION>
			<TYPE>E</TYPE>
			<CODE>06199</CODE>
			<MESSAGE>Berechtigung fehlt: Bestellung ändern Einkaufsorg.</MESSAGE>
			<LOG_NO/>
			<LOG_MSG_NO>000000</LOG_MSG_NO>
			<MESSAGE_V1>Bestellung</MESSAGE_V1>
			<MESSAGE_V2>ändern</MESSAGE_V2>
			<MESSAGE_V3>Einkaufsorg.</MESSAGE_V3>
			<MESSAGE_V4>1201</MESSAGE_V4>
		</EXCEPTION>
	</item>
</node>

Das JavaScript sieht so aus:

Code:
var parseXml;

	if (typeof window.DOMParser != "undefined") {
	    parseXml = function(xmlStr) {
	        return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
	    };
	} else if (typeof window.ActiveXObject != "undefined" && new window.ActiveXObject("Microsoft.XMLDOM")) {
	    parseXml = function(xmlStr) {
	        var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
	        xmlDoc.async = "false";
	        xmlDoc.loadXML(xmlStr);
	        return xmlDoc;
	    };
	} else {
	    throw new Error("Kein XML-Parser verfügbar");
	}

	var xml = parseXml(XML-Quelle);
	alert(xml.documentElement.firstChild.firstChild.lastChild.nodeName);

Wie kann ich also die Werte von <MESSAGE> auslesen? Ich finde keine Methode oder Funktion in JavaScript und laufe langsam Amok oder schlage die Zimmerpflanze :D

PS: Und bitte entschuldigt den Anfängercode in Zeile 19, ich wollte es nur schnell testen...

Grüße
Speedy
 
Keine Ahnung was für ein Knotentyp das ist, aber in HTML entspricht das innerHTML.

Knall mal ein debugger; ins JavaScript unter Zeile 18, öffne die Entwicklerkonsole und lass dir mal per Code Insight anzeigen, welche Properties das angesprochene Element besitzt. Ein console.dir( xml.documentElement.firstChild.firstChild.lastChild ); hilft dir auch bei der Wegfindung.
 
Mit innerHTML kommt da "undefined" raus.

Laut Ausgabe ist das Knotentyp 1. Siehe hier mal den Log der Konsole:


Code:
clientHeight : 0,
	clientLeft : 0,
	clientTop : 0,
	clientWidth : 0,
	scrollHeight : 0,
	scrollLeft : 0,
	scrollTop : 0,
	scrollWidth : 0,
	tagName : "MESSAGE_V4",
	childElementCount : 0,
	firstElementChild : null,
	lastElementChild : null,
	nextElementSibling : null,
	previousElementSibling : [object Element],
	getAttribute :  function getAttribute() {     [native code] } ,
	getAttributeNS :  function getAttributeNS() {     [native code] } ,
	getAttributeNode :  function getAttributeNode() {     [native code] } ,
	getAttributeNodeNS :  function getAttributeNodeNS() {     [native code] } ,
	getBoundingClientRect :  function getBoundingClientRect() {     [native code] } ,
	getClientRects :  function getClientRects() {     [native code] } ,
	getElementsByTagName :  function getElementsByTagName() {     [native code] } ,
	getElementsByTagNameNS :  function getElementsByTagNameNS() {     [native code] } ,
	hasAttribute :  function hasAttribute() {     [native code] } ,
	hasAttributeNS :  function hasAttributeNS() {     [native code] } ,
	removeAttribute :  function removeAttribute() {     [native code] } ,
	removeAttributeNS :  function removeAttributeNS() {     [native code] } ,
	removeAttributeNode :  function removeAttributeNode() {     [native code] } ,
	setAttribute :  function setAttribute() {     [native code] } ,
	setAttributeNS :  function setAttributeNS() {     [native code] } ,
	setAttributeNode :  function setAttributeNode() {     [native code] } ,
	setAttributeNodeNS :  function setAttributeNodeNS() {     [native code] } ,
	fireEvent :  function fireEvent() {     [native code] } ,
	msMatchesSelector :  function msMatchesSelector() {     [native code] } ,
	querySelector :  function querySelector() {     [native code] } ,
	querySelectorAll :  function querySelectorAll() {     [native code] } ,
	attributes : [object NamedNodeMap],
	childNodes : [object NodeList],
	firstChild : [object Text],
	lastChild : [object Text],
	localName : "MESSAGE_V4",
	namespaceURI : null,
	nextSibling : null,
	nodeName : "MESSAGE_V4",
	nodeType : 1,
	nodeValue : null,
	ownerDocument : [object Document],
	parentNode : [object Element],
	prefix : null,
	previousSibling : [object Element],
	textContent : "1201",
	addEventListener :  function addEventListener() {     [native code] } ,
	dispatchEvent :  function dispatchEvent() {     [native code] } ,
	removeEventListener :  function removeEventListener() {     [native code] } ,
	appendChild :  function appendChild() {     [native code] } ,
	cloneNode :  function cloneNode() {     [native code] } ,
	compareDocumentPosition :  function compareDocumentPosition() {     [native code] } ,
	hasAttributes :  function hasAttributes() {     [native code] } ,
	hasChildNodes :  function hasChildNodes() {     [native code] } ,
	insertBefore :  function insertBefore() {     [native code] } ,
	isDefaultNamespace :  function isDefaultNamespace() {     [native code] } ,
	isEqualNode :  function isEqualNode() {     [native code] } ,
	isSameNode :  function isSameNode() {     [native code] } ,
	isSupported :  function isSupported() {     [native code] } ,
	lookupNamespaceURI :  function lookupNamespaceURI() {     [native code] } ,
	lookupPrefix :  function lookupPrefix() {     [native code] } ,
	normalize :  function normalize() {     [native code] } ,
	removeChild :  function removeChild() {     [native code] } ,
	replaceChild :  function replaceChild() {     [native code] } ,
	ATTRIBUTE_NODE : 2,
	CDATA_SECTION_NODE : 4,
	COMMENT_NODE : 8,
	DOCUMENT_FRAGMENT_NODE : 11,
	DOCUMENT_NODE : 9,
	DOCUMENT_POSITION_CONTAINED_BY : 16,
	DOCUMENT_POSITION_CONTAINS : 8,
	DOCUMENT_POSITION_DISCONNECTED : 1,
	DOCUMENT_POSITION_FOLLOWING : 4,
	DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC : 32,
	DOCUMENT_POSITION_PRECEDING : 2,
	DOCUMENT_TYPE_NODE : 10,
	ELEMENT_NODE : 1,
	ENTITY_NODE : 6,
	ENTITY_REFERENCE_NODE : 5,
	NOTATION_NODE : 12,
	PROCESSING_INSTRUCTION_NODE : 7,
	TEXT_NODE : 3

Ist Code Insight ein Entwicklertool von einem Browser. Ich muss hier zwangsläufig den Internet Explorer 9 einsetzen und habe dort keine Funktion mit diesem Namen gefunden.

Der Inhalt ist definitiv nicht null (bzw. nicht leer), die Zahlen 1201 sind eingetragen.
 
Kennt man auch unter Code Completion, wenn dir das mehr sagt (wird Intelli Sight in Visual Studio genannt). Wenn du halt ein Objekt vor dir hast und "." drückst, erscheinen dann automatisch alle verfügbaren Properties. Ich glaub der IE 9 hatte sowas noch nicht drin.

Bei dir ist es die Property textContent, wo der Wert drin steht. Also geht der Zugriff über xml.documentElement.firstChild.firstChild.lastChild.textContent.
 
Ha! Cool, es hat wunderbar funktioniert.
Jetzt weiß ich auch, was du mit Code Insight meinst. Im IE9 habe ich das bisher noch nicht gesehen, und Chrome darf ich nicht nutzen...
Also, tausend Dank für die Hilfe, jetzt darf die Zimmerpflanze weiterleben :D
 
Neben
Code:
textNode.textContent
kannst du auch
Code:
textNode.data
oder
Code:
 textNode.nodeValue
benutzen. Was genau die Unterschiede sind, musst du selbst ausprobieren.
 
Zurück
Oben