How to Import Xml String in a PHP Domdocument

How to import XML string in a php DOMDocument

The problem is DOM does not know that it should consider the XHTML DTD unless you validated the document against it. Unless you do that, DOM doesnt know any entities defined in the DTD, nor any other rules in it. Fortunately, we sorted out how to do the validation in that other question, so armed with that knowledge you can do

$document->validate(); // anywhere before importing the other DOM

And then import with

$fragment = $document->createDocumentFragment();
$fragment->appendXML('<h1>Hello</h1><p>Hello World</p>');
$document->getElementsByTagName('body')->item(0)->appendChild($fragment);
$document->formatOutput = TRUE;
echo $document->saveXml();

outputs:

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>My bweb page</title>
</head>
<body>
<h1>Hello</h1>
<p>Hello World</p>
</body>
</html>

The other way to import XML into another DOM is to use

$one = new DOMDocument;
$two = new DOMDocument;
$one->loadXml('<root><foo>one</foo></root>');
$two->loadXml('<root><bar><sub>two</sub></bar></root>');
$bar = $two->documentElement->firstChild; // we want to import the bar tree
$one->documentElement->appendChild($one->importNode($bar, TRUE));
echo $one->saveXml();

outputs:

<?xml version="1.0"?>
<root><foo>one</foo><bar><sub>two</sub></bar></root>

However, this cannot work with

<h1>Hello</h1><p>Hello World</p>

because when you load a document into DOM, DOM will overwrite everything you told it before about the document. Thus, when using load, libxml (and thus SimpleXml, DOM and XMLReader) does (do) not know you mean XHTML. And it does not know any entities defined in it and will fuzz about them instead. But even if the string would not contain the entity, it is not valid XML, because it lacks a root node. That's why you use the fragment.

PHP DOMDocument: how to incorporate a string of xml?

Here a two possible solutions:

Import From A Source Document

This works only if the XML string is a valid document. You need to import the document element, or any descendant of it. Depends on the part you would like to add to the target document.

$xml = "<child>text</child>";

$source = new DOMDocument();
$source->loadXml($xml);

$target = new DOMDocument();
$root = $target->appendChild($target->createElement('root'));
$root->appendChild($target->importNode($source->documentElement, TRUE));

echo $target->saveXml();

Output:

<?xml version="1.0"?>
<root><child>text</child></root>

Use A Document Fragment

This works for any valid XML fragment. Even if it has no root node.

$xml = "text<child>text</child>";

$target = new DOMDocument();
$root = $target->appendChild($target->createElement('root'));

$fragment = $target->createDocumentFragment();
$fragment->appendXml($xml);
$root->appendChild($fragment);

echo $target->saveXml();

Output:

<?xml version="1.0"?>
<root>text<child>text</child></root>

How to append XML string into a DOMDocument object?

You're trying to append the original node - not the imported one.

$Signature_node = $getToken_objeto->importNode(
$Signature_nodeList->item(0), true
);

You're trying to append the node to the document, but an XML document can only have a single document element and it already has one. You can append it to the document element:

$getToken_objeto->documentElement->appendChild($Signature_node);

But PHP can load XML fragments directly into a DOMDocumentFragment.

$xml = '<getToken>...</getToken>';
$fragmentXml = '<Signature>...</Signature>';

$dom = new DOMDocument();
$dom->loadXML($xml);
$xpath = new DOMXpath($dom);

$fragment = $dom->createDocumentFragment();
$fragment->appendXml($fragmentXml);

$xpath
->evaluate('//getToken')
->item(0)
->appendChild($fragment);

echo $dom->saveXml();

How to parse a XML string as document object(DOM) in php

DOMDocument::load — Load XML from a file

As you can see in php manual, load() method only work for loading XML from a file.

If you want to load from string, use DOMDocument::loadXML that load XML from string.

Read XML File with DOMDocument in php

The XML uses a namespace, so you should use the namespace aware methods. They have the suffix _NS.

$tns = 'http://www.testgroup.com/TestPDM';
$document = new DOMDocument();
$document->loadXml($xml);
foreach ($document->getElementsByTagNameNS($tns, "pdmNumber") as $node) {
var_dump($node->textContent);
}

Output:

string(6) "654321"

A better option is to use Xpath expression. They allow a more comfortable access to DOM nodes. In this case you have to register a prefix for the namespace that you can use in the Xpath expression:

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$xpath->registerNamespace('t', 'http://www.testgroup.com/TestPDM');

var_dump(
$xpath->evaluate('string(/t:getPDMNumber/t:getPDMNumberResponse/t:pdmNumber)')
);

how to add/append multiple string data in DOMDocument xml using php

You current source loads a fragment string as a separate document. This will fail because an wellformed XML document can only have a single document element node.

A document fragment is a special node type in DOM, so you can create and append it like any other node. It has a method appendXml() to parse the XML fragment string.

Here is a simple example:

$document = new DOMDocument();
$document->loadXml('<catalog/>');

$fragment = $document->createDocumentFragment();
$fragment->appendXml(
'<book><title>First</title></book>
<book><title>Second</title></book>'
);

$document->documentElement->appendChild($fragment);

echo $document->saveXml();

Output:

<?xml version="1.0"?>
<catalog><book><title>First</title></book>
<book><title>Second</title></book></catalog>


Related Topics



Leave a reply



Submit