Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : DOM Functions : DOMDocument->createDocumentFragment()

DOMDocument->createDocumentFragment()

Create new document fragment ()

Examples ( Source code ) » DOMDocument->createDocumentFragment()

//my_employee.xml
    <?xml version="1.0" encoding="UTF-8"?>
    
    <my_employee>
    
                            <my_employeename>test</my_employeename>
    
                            <my_employeeaddress>test123</my_employeeaddress>
    
                            <SSN>12343456</SSN>
    
                            <company>TOPXML</company>
    
    </my_employee>
PHP Code:

<?php

    $doc 
= new DomDocument;

// Load the xml file into DOMDocument

   
$doc->Load('my_employee.xml');

// We retrieve the attribute named id of the my_employee element

   
$my_employee $doc->getElementsByTagName('my_employee')->item(0);

//Create a new document fragment

   
$newDocFragment $doc ->createDocumentFragment();

//Create an element and append it to the document fragment node

   
$newDocFragment->appendChild($doc->createElement("newElement"));

//append the new node(Document Fragment) into the my_employee element

   
$my_employee -> appendChild($newDocFragment);

//save the DOMDocument into a file

    
$test $doc->save("saveFile.xml");

    echo 
"<B>DOMDocument->createDocumentFragment example<B>"

 
?>
Output:

The Content of saveFile.xml is

    <?xml version="1.0" encoding="UTF-8"?>
    
    <my_employee>
    
                   <my_employeename>test</my_employeename>
    
                   <my_employeeaddress>test123</my_employeeaddress>
    
                   <SSN>12343456</SSN>
    
                   <company>TOPXML</company>
    
                   <newElement/>
    
    </my_employee>   

Related Examples ( Source code ) » dom domdocument createdocumentfragment



Code Examples / Notes » dom domdocument createdocumentfragment

02-dec-2005 08:58

You can append document fragments to documents. This will append their nodes instead of fragment node itself.
So if you're asking yourself "how do I make function that returns more than one node" - that is the answer.


xavier.pinard

The interest of fragment is I think :
- only nodes inside the fragment are inserted,
  not the fragment itself when you insert the fragment
- you can append String litteral with the method
  DOMDocumentFragment->appendXML()
So, a fragment may look like this :
part of document<li>bla bla </li> ...
This fragment is describe with a fragment description
that may look like this is you use
it as an "XML Fragment Interchange" :
<f:fcs xmlns:f="http://www.w3.org/2001/02/xml-fragment"
[...]
<myOriginalStructure>
[...]
<f:fragbody/>
[...]
</myOriginalStructure>
cf :: http://www.w3.org/TR/xml-fragment
An example could be for "XML Fragment Interchange":
$dom = new DomDocument();
$frag = $dom->createDocumentFragment();
$fragbody = $dom->createElementNS(
'http://www.w3.org/2001/02/xml-package',
'p:body'
);
$frag->appendXML(
'test fragment<ilo>data</ilo> +
an object property +
something else '
);
$fragbody->appendChild($frag);
$dom->appendChild($fragbody);
var_dump($dom->saveXML());


naonak

Example :
$dom = new DomDocument;
$frag = $dom->createDocumentFragment();
$fragment= $dom->createElement( 'fragment' );
$frag->appendChild( $fragment);
$domNode = $dom2->importNode($frag, true);
$dom2->appendChild( $domNode );
print_r($dom2->saveXML());


jb

DOMDocumentFragment can be very useful with XPath: you may need to apply an XPath expression to a list of nodes.
$dom_doc = new DOMDocument();
$dom_fragment = $dom_doc->createDocumentFragment();
$dom_element_a1 = $dom_fragment->appendChild($dom_doc->createElement('a', 'first node'));
$dom_element_a2 = $dom_fragment->appendChild($dom_doc->createElement('a', 'second node'));
$xpath = new DOMXpath($dom_doc);
$nodes = $xpath->query('a', $dom_fragment);
foreach ($nodes as $node) {
   echo $node->textContent . "\n";
}
This way you do not need to embed the nodes within a parent element that could be returned by the xpath expression when you do not want this to happen.


Change Language


Follow Navioo On Twitter
DOMAttr->__construct()
DOMAttr->isId()
DOMCharacterData->appendData()
DOMCharacterData->deleteData()
DOMCharacterData->insertData()
DOMCharacterData->replaceData()
DOMCharacterData->substringData()
DOMComment->__construct()
DOMDocument->__construct()
DOMDocument->createAttribute()
DOMDocument->createAttributeNS()
DOMDocument->createCDATASection()
DOMDocument->createComment()
DOMDocument->createDocumentFragment()
DOMDocument->createElement()
DOMDocument->createElementNS()
DOMDocument->createEntityReference()
DOMDocument->createProcessingInstruction()
DOMDocument->createTextNode()
DOMDocument->getElementById()
DOMDocument->getElementsByTagName()
DOMDocument->getElementsByTagNameNS()
DOMDocument->importNode()
DOMDocument->load()
DOMDocument->loadHTML()
DOMDocument->loadHTMLFile()
DOMDocument->loadXML()
DOMDocument->normalizeDocument()
DOMDocument->registerNodeClass()
DOMDocument->relaxNGValidate()
DOMDocument->relaxNGValidateSource()
DOMDocument->save()
DOMDocument->saveHTML()
DOMDocument->saveHTMLFile()
DOMDocument->saveXML()
DOMDocument->schemaValidate()
DOMDocument->schemaValidateSource()
DOMDocument->validate()
DOMDocument->xinclude()
DOMDocumentFragment->appendXML()
DOMElement->__construct()
DOMElement->getAttribute()
DOMElement->getAttributeNode()
DOMElement->getAttributeNodeNS()
DOMElement->getAttributeNS()
DOMElement->getElementsByTagName()
DOMElement->getElementsByTagNameNS()
DOMElement->hasAttribute()
DOMElement->hasAttributeNS()
DOMElement->removeAttribute()
DOMElement->removeAttributeNode()
DOMElement->removeAttributeNS()
DOMElement->setAttribute()
DOMElement->setAttributeNode()
DOMElement->setAttributeNodeNS()
DOMElement->setAttributeNS()
DOMElement->setIdAttribute()
DOMElement->setIdAttributeNode()
DOMElement->setIdAttributeNS()
DOMEntityReference->__construct()
DOMImplementation->__construct()
DOMImplementation->createDocument()
DOMImplementation->createDocumentType()
DOMImplementation->hasFeature()
DOMNamedNodeMap->getNamedItem()
DOMNamedNodeMap->getNamedItemNS()
DOMNamedNodeMap->item()
DOMNode->appendChild()
DOMNode->cloneNode()
DOMNode->hasAttributes()
DOMNode->hasChildNodes()
DOMNode->insertBefore()
DOMNode->isDefaultNamespace()
DOMNode->isSameNode()
DOMNode->isSupported()
DOMNode->lookupNamespaceURI()
DOMNode->lookupPrefix()
DOMNode->normalize()
DOMNode->removeChild()
DOMNode->replaceChild()
DOMNodelist->item()
DOMProcessingInstruction->__construct()
DOMText->__construct()
DOMText->isWhitespaceInElementContent()
DOMText->splitText()
DOMXPath->__construct()
DOMXPath->evaluate()
DOMXPath->query()
DOMXPath->registerNamespace()
dom_import_simplexml
eXTReMe Tracker