Asked  7 Months ago    Answers:  5   Viewed   37 times

I have a previously generated XML like this:







I need to be able to insert an element in between the <intro> and the <shop> elements


$section = $dom->documentElement->appendChild($dom->createElement('section'));

will just create the element within <newsletter> .

I assumed this would be fairly simple , but cannot seem to find a solution .




You might try this; I didn't test it, but the solution comes from using insertBefore instead of appendChild.

$shop = $dom->getElementsByTagName("shop")->item(0);
$section = $dom->documentElement->insertBefore($dom->createElement('section'),$shop);
Wednesday, March 31, 2021
answered 7 Months ago

The answer is that it doesn't actually need any escaping according to the spec (skipping the mentions of CDATA):

The ampersand character (&) and the left angle bracket (<) must not appear in their literal form (...) If they are needed elsewhere, they must be escaped using either numeric character references or the strings " &amp; " and " &lt; " respectively. The right angle bracket (>) may be represented using the string " &gt; " (...)

To allow attribute values to contain both single and double quotes, the apostrophe or single-quote character (') may be represented as " &apos; ", and the double-quote character (") as " &quot; ".

You can verify this easily by using createTextNode() to perform the correct escaping:

$dom = new DOMDocument;
$e = $dom->createElement('description');
$content = 'single quote: ', double quote: ", opening tag: <, ampersand: &, closing tag: >';
$t = $dom->createTextNode($content);

echo $dom->saveXML();


<?xml version="1.0"?>
<description>single quote: ', double quote: ", opening tag: &lt;, ampersand: &amp;, closing tag: &gt;</description>
Saturday, May 29, 2021
answered 5 Months ago

Using recursion, you can create a brand new document based on the input, solving all your points at once:



$input = file_get_contents('');
$inputDoc = new DOMDocument();

$outputDoc = new DOMDocument("1.0", "utf-8");

function ConvertUserToItem($outputDoc, $inputNode, $outputNode)
    if ($inputNode->hasChildNodes())
        foreach ($inputNode->childNodes as $inputChild)
            if (strtolower($inputChild->nodeName) == "user")
                $outputChild = $outputDoc->createElement("item");
                // read input attributes and convert them to nodes
                if ($inputChild->hasAttributes())
                    $outputContent = $outputDoc->createElement("content");
                    foreach ($inputChild->attributes as $attribute)
                        if (strtolower($attribute->name) != "id")
                            $outputContent->appendChild($outputDoc->createElement($attribute->name, $attribute->value));
                            $outputChild->setAttribute($attribute->name, $attribute->value);
                // recursive call
                ConvertUserToItem($outputDoc, $inputChild, $outputChild);

ConvertUserToItem($outputDoc, $inputDoc->documentElement, $outputDoc->documentElement);

header("Content-Type: text/xml; charset=" . $outputDoc->encoding);
echo $outputDoc->saveXML();


<?xml version="1.0" encoding="utf-8"?>
    <item id="41">
            <fullname>Test Name</fullname>
        <item id="61">
                <fullname>Test Name</fullname>
Saturday, May 29, 2021
answered 5 Months ago

The content property states:

Authors may include newlines in the generated content by writing the "A" escape sequence in one of the strings after the 'content' property. This inserted line break is still subject to the 'white-space' property. See "Strings" and "Characters and case" for more information on the "A" escape sequence.

So you can use:

#headerAgentInfoDetailsPhone:after {
  content:"Office: XXXXX A Mobile: YYYYY ";
  white-space: pre; /* or pre-wrap */

When escaping arbitrary strings, however, it's advisable to use 0000a instead of A, because any number or [a-f] character followed by the new line may give unpredictable results:

function addTextToStyle(id, text) {
  return `#${id}::after { content: "${text.replace(/"/g, '\"').replace(/n/g, '\00000a')} }"`;
Wednesday, June 2, 2021
answered 5 Months ago

You need to import any node to append it to another document:

$departmentArray->item($i)->appendChild( $doc->importNode( $employee, true ) );
Friday, August 6, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :