diff --git a/lib/SAML2/Assertion.php b/lib/SAML2/Assertion.php
index 6bbe5621f0f7188fe00fbaa361223cdc3f567f73..4144964d34707f3ad9ee58b64a80729d3e2f37fa 100644
--- a/lib/SAML2/Assertion.php
+++ b/lib/SAML2/Assertion.php
@@ -995,9 +995,7 @@ class SAML2_Assertion implements SAML2_SignedElement {
 		$root->setAttribute('Version', '2.0');
 		$root->setAttribute('IssueInstant', gmdate('Y-m-d\TH:i:s\Z', $this->issueInstant));
 
-		$issuer = $document->createElementNS(SAML2_Const::NS_SAML, 'saml:Issuer');
-		$issuer->appendChild($document->createTextNode($this->issuer));
-		$root->appendChild($issuer);
+		$issuer = SAML2_Utils::addString($root, SAML2_Const::NS_SAML, 'saml:Issuer', $this->issuer);
 
 		$this->addSubject($root);
 		$this->addConditions($root);
@@ -1111,10 +1109,7 @@ class SAML2_Assertion implements SAML2_SignedElement {
 		$ac = $document->createElementNS(SAML2_Const::NS_SAML, 'saml:AuthnContext');
 		$as->appendChild($ac);
 
-		$accr = $document->createElementNS(SAML2_Const::NS_SAML, 'saml:AuthnContextClassRef');
-		$ac->appendChild($accr);
-
-		$accr->appendChild($document->createTextNode($this->authnContext));
+		SAML2_Utils::addString($ac, SAML2_Const::NS_SAML, 'saml:AuthnContextClassRef', $this->authnContext);
 	}
 
 
diff --git a/lib/SAML2/AttributeQuery.php b/lib/SAML2/AttributeQuery.php
index 85e617455f42e39031539aacebff0dd7ca96705b..09b70b7841a64c41784b752fe5ebd93b5f30729b 100644
--- a/lib/SAML2/AttributeQuery.php
+++ b/lib/SAML2/AttributeQuery.php
@@ -162,12 +162,10 @@ class SAML2_AttributeQuery extends SAML2_SubjectQuery {
 					$type = NULL;
 				}
 
-				$attributeValue = $root->ownerDocument->createElementNS(SAML2_Const::NS_SAML, 'saml:AttributeValue');
-				$attribute->appendChild($attributeValue);
+				$attributeValue = SAML2_Utils::addString($attribute, SAML2_Const::NS_SAML, 'saml:AttributeValue', $value);
 				if ($type !== NULL) {
 					$attributeValue->setAttributeNS(SAML2_Const::NS_XSI, 'xsi:type', $type);
 				}
-				$attributeValue->appendChild($root->ownerDocument->createTextNode($value));
 			}
 		}
 
diff --git a/lib/SAML2/AuthnRequest.php b/lib/SAML2/AuthnRequest.php
index 094a0e4bd052c9ee17919883f21ce3ba2b274e9d..10c7defe360ccc888f5a0e792c07a635676b03f0 100644
--- a/lib/SAML2/AuthnRequest.php
+++ b/lib/SAML2/AuthnRequest.php
@@ -348,9 +348,7 @@ class SAML2_AuthnRequest extends SAML2_Request {
 				$e->setAttribute('Comparison', $rac['Comparison']);
 			}
 			foreach ($rac['AuthnContextClassRef'] as $accr) {
-				$i = $this->document->createElementNS(SAML2_Const::NS_SAML, 'AuthnContextClassRef');
-				$i->appendChild($this->document->createTextNode($accr));
-				$e->appendChild($i);
+				SAML2_Utils::addString($e, SAML2_Const::NS_SAML, 'AuthnContextClassRef', $accr);
 			}
 		}
 
diff --git a/lib/SAML2/LogoutRequest.php b/lib/SAML2/LogoutRequest.php
index 7cfc7204b03c5dda6d6267fbb15b24d2ff370c66..c7a8a9c6eef8ca6c028dff867deda44c85568308 100644
--- a/lib/SAML2/LogoutRequest.php
+++ b/lib/SAML2/LogoutRequest.php
@@ -109,9 +109,7 @@ class SAML2_LogoutRequest extends SAML2_Request {
 		SAML2_Utils::addNameId($root, $this->nameId);
 
 		if ($this->sessionIndex !== NULL) {
-			$sessionIndex = $this->document->createElementNS(SAML2_Const::NS_SAMLP, 'SessionIndex');
-			$sessionIndex->appendChild($this->document->createTextNode($this->sessionIndex));
-			$root->appendChild($sessionIndex);
+			SAML2_Utils::addString($root, SAML2_Const::NS_SAMLP, 'SessionIndex', $this->sessionIndex);
 		}
 
 		return $root;
diff --git a/lib/SAML2/Message.php b/lib/SAML2/Message.php
index f17e898263cb9be19729f43264e88826e2dcb4bd..c2d12a8ea355623585124f0e345a20f08054e29c 100644
--- a/lib/SAML2/Message.php
+++ b/lib/SAML2/Message.php
@@ -357,9 +357,7 @@ abstract class SAML2_Message implements SAML2_SignedElement {
 		}
 
 		if ($this->issuer !== NULL) {
-			$issuer = $this->document->createElementNS(SAML2_Const::NS_SAML, 'saml:Issuer');
-			$issuer->appendChild($this->document->createTextNode($this->issuer));
-			$root->appendChild($issuer);
+			SAML2_Utils::addString($root, SAML2_Const::NS_SAML, 'saml:Issuer', $this->issuer);
 		}
 
 		return $root;
diff --git a/lib/SAML2/StatusResponse.php b/lib/SAML2/StatusResponse.php
index 6b9f7c760a9f7db414457b1025b9cc40c68b2cea..bc264fbf0e4629e09ea2d7957329dfe251d658ae 100644
--- a/lib/SAML2/StatusResponse.php
+++ b/lib/SAML2/StatusResponse.php
@@ -181,9 +181,7 @@ abstract class SAML2_StatusResponse extends SAML2_Message {
 		}
 
 		if (!is_null($this->status['Message'])) {
-			$statusMessage = $this->document->createElementNS(SAML2_Const::NS_SAMLP, 'StatusMessage');
-			$statusMessage->appendChild($this->document->createTextNode($this->status['Message']));
-			$status->appendChild($statusMessage);
+			SAML2_Utils::addString($status, SAML2_Const::NS_SAMLP, 'StatusMessage', $this->status['Message']);
 		}
 
 		return $root;
diff --git a/lib/SAML2/Utils.php b/lib/SAML2/Utils.php
index 8cf84426a599df5ed1bbe326d91d08d33f18ff04..42b5c5544723eda2e49a8649428e08409a56e43d 100644
--- a/lib/SAML2/Utils.php
+++ b/lib/SAML2/Utils.php
@@ -175,8 +175,7 @@ class SAML2_Utils {
 	public static function addNameId(DOMElement $node, array $nameId) {
 		assert('array_key_exists("Value", $nameId)');
 
-		$xml = $node->ownerDocument->createElementNS(SAML2_Const::NS_SAML, 'saml:NameID');
-		$node->appendChild($xml);
+		$xml = SAML2_Utils::addString($node, SAML2_Const::NS_SAML, 'saml:NameID', $nameId['Value']);
 
 		if (array_key_exists('NameQualifier', $nameId) && $nameId['NameQualifier'] !== NULL) {
 			$xml->setAttribute('NameQualifier', $nameId['NameQualifier']);
@@ -187,8 +186,6 @@ class SAML2_Utils {
 		if (array_key_exists('Format', $nameId) && $nameId['Format'] !== NULL) {
 			$xml->setAttribute('Format', $nameId['Format']);
 		}
-
-		$xml->appendChild($node->ownerDocument->createTextNode($nameId['Value']));
 	}
 
 
@@ -367,4 +364,28 @@ class SAML2_Utils {
 		return $ret;
 	}
 
+
+	/**
+	 * Append string element.
+	 *
+	 * @param DOMElement $parent  The parent element we should append the new nodes to.
+	 * @param string $namespace  The namespace of the created element.
+	 * @param string $name  The name of the created element.
+	 * @param string $value  The value of the element.
+	 * @return DOMElement  The generated element.
+	 */
+	public static function addString(DOMElement $parent, $namespace, $name, $value) {
+		assert('is_string($namespace)');
+		assert('is_string($name)');
+		assert('is_string($value)');
+
+		$doc = $parent->ownerDocument;
+
+		$n = $doc->createElementNS($namespace, $name);
+		$n->appendChild($doc->createTextNode($value));
+		$parent->appendChild($n);
+
+		return $n;
+	}
+
 }