git » sdk » commit 2cd3ba3

Support for building stanzas out of haxe XML

author Stephen Paul Weber
2023-09-27 18:12:30 UTC
committer Stephen Paul Weber
2023-09-27 18:12:30 UTC
parent 7d94163144a1505371202481dcb7799afb5745d1

Support for building stanzas out of haxe XML

xmpp/Stanza.hx +23 -0

diff --git a/xmpp/Stanza.hx b/xmpp/Stanza.hx
index eb9fe80..c6121af 100644
--- a/xmpp/Stanza.hx
+++ b/xmpp/Stanza.hx
@@ -3,6 +3,7 @@ package xmpp;
 import haxe.DynamicAccess;
 import haxe.Exception;
 import haxe.ds.StringMap;
+import Xml;
 
 enum Node {
 	Element(stanza:Stanza);
@@ -72,6 +73,28 @@ class Stanza implements NodeInterface {
 		return this.serialize();
 	}
 
+	public static function fromXml(el:Xml):Stanza {
+		if(el.nodeType == XmlType.Document) {
+			return fromXml(el.firstElement());
+		}
+
+		var attrs: DynamicAccess<String> = {};
+		for (a in el.attributes()) {
+			attrs.set(a, el.get(a));
+		}
+		var stanza = new Stanza(el.nodeName, attrs);
+		for (child in el) {
+			if(child.nodeType == XmlType.Element) {
+				stanza.addChild(fromXml(child));
+			} else if (child.nodeType == XmlType.ProcessingInstruction || child.nodeType == XmlType.DocType || child.nodeType == XmlType.Comment) {
+				// Ignore non-operative XML items
+			} else {
+				stanza.text(child.nodeValue);
+			}
+		}
+		return stanza;
+	}
+
 	public function tag(name:String, ?attr:DynamicAccess<String>) {
 		var child = new Stanza(name, attr);
 		this.last_added.addDirectChild(Element(child));