Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 24
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
Gratulacje i dzięki! Trafiłem tu przypadkiem poszukując informacji na temat php+mysql. Wiele polskich stron powiela identyczne przykłady, klonuje te same kursy i lekcje... ten serwis okazał sie inny. Zasada "problem - rozwiazanie - wyjaśnienie" zdaje egzamin - zapewnia jasną, jednoznaczną i pewną pomoc w konkretnym przypadku. Porady są warte swojej ceny, przede wszystkim ze względu na przyjazną (także dla początkujących) formę i treść oraz bogate i stale powiększane zasoby. Polecam i pozdrawiam!

Kamil Dmowski
Polski Czerwony Krzyż

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

XXVII. DOM Functions

Wstęp

The DOM extension is the replacement for the DOM XML extension from PHP 4. The extension still contains many old functions, but they should no longer be used. In particular, functions that are not object-oriented should be avoided.

The extension allows you to operate on an XML document with the DOM API.

Instalacja

By używać tych funkcji, nie trzeba niczego instalować. Są one częścią jądra PHP.

Klasy predefiniowane

The API of the module follows the DOM Level 2 standard as closely as possible. Consequently, the API is fully object-oriented. It is a good idea to have the DOM standard available when using this module.

This module defines a number of classes, which are explained in the following tables. Classes with an equivalent in the DOM standard are named DOMxxx.

DOMAttr

Extends DOMNode.

Konstructor

Metody

Właściwości

Tabela 1.

NameTypeRead-onlyDescription
namestringyesThe name of the attribute
ownerElementDOMElementyesThe element which contains the attribute
schemaTypeInfoboolyesNot implemented yet, always return TRUE
specifiedboolyesNot implemented yet, always return TRUE
valuestringnoThe value of the attribute

DOMCharacterData

Extends DOMNode.

Metody

Właściwości

Tabela 2.

NameTypeRead-onlyDescription
datastringnoThe contents of the node
lengthintyesThe length of the contents

DOMComment

Extends DOMCharacterData.

Konstructor

DOMDocument

Extends DOMNode.

Konstructor

Metody

Właściwości

Tabela 3.

NameTypeRead-onlyDescription
actualEncodingstringyes 
configDOMConfigurationyes 
doctypeDOMDocumentTypeyes The Document Type Declaration associated with this document.
documentElementDOMElementyes This is a convenience attribute that allows direct access to the child node that is the document element of the document.
documentURIstringno The location of the document or NULL if undefined.
encodingstringno 
formatOutputboolno 
implementationDOMImplementationyes The DOMImplementation object that handles this document.
preserveWhiteSpaceboolnoDo not remove redundant white space. Default to TRUE.
recoverboolno 
resolveExternalsboolno Set it to TRUE to load external entities from a doctype declaration. This is useful for including character entities in your XML document.
standaloneboolno 
strictErrorCheckingboolnoThrows DOMException on errors. Default to TRUE.
substituteEntitiesboolno 
validateOnParseboolnoLoads and validates against the DTD. Default to FALSE.
versionstringno 
xmlEncodingstringyes An attribute specifying, as part of the XML declaration, the encoding of this document. This is NULL when unspecified or when it is not known, such as when the Document was created in memory.
xmlStandaloneboolno An attribute specifying, as part of the XML declaration, whether this document is standalone. This is FALSE when unspecified.
xmlVersionstringno An attribute specifying, as part of the XML declaration, the version number of this document. If there is no declaration and if this document supports the "XML" feature, the value is "1.0".

DOMDocumentType

Extends DOMNode

Each DOMDocument has a doctype attribute whose value is either NULL or a DOMDocumentType object.

Właściwości

Tabela 4.

NameTypeRead-onlyDescription
publicIdstringyesThe public identifier of the external subset.
systemIdstringyesThe system identifier of the external subset. This may be an absolute URI or not.
namestringyesThe name of DTD; i.e., the name immediately following the DOCTYPE keyword.
entitiesDOMNamedNodeMapyes A DOMNamedNodeMap containing the general entities, both external and internal, declared in the DTD.
notationsDOMNamedNodeMapyes A DOMNamedNodeMap containing the notations declared in the DTD.
internalSubsetstringyes The internal subset as a string, or null if there is none. This is does not contain the delimiting square brackets.

DOMElement

Extends DOMNode.

Konstructor

Metody

Właściwości

Tabela 5.

NameTypeRead-onlyDescription
schemaTypeInfoboolyesNot implemented yet, always return TRUE
tagNamestringyesThe element name

DOMEntity

Extends DOMNode

This interface represents a known entity, either parsed or unparsed, in an XML document.

Właściwości

Tabela 6.

NameTypeRead-onlyDescription
publicIdstringyes The public identifier associated with the entity if specified, and NULL otherwise.
systemIdstringyes The system identifier associated with the entity if specified, and NULL otherwise. This may be an absolute URI or not.
notationNamestringyes For unparsed entities, the name of the notation for the entity. For parsed entities, this is NULL.
actualEncodingstringno An attribute specifying the encoding used for this entity at the time of parsing, when it is an external parsed entity. This is NULL if it an entity from the internal subset or if it is not known.
encodingstringyes An attribute specifying, as part of the text declaration, the encoding of this entity, when it is an external parsed entity. This is NULL otherwise.
versionstringyes An attribute specifying, as part of the text declaration, the version number of this entity, when it is an external parsed entity. This is NULL otherwise.

DOMEntityReference

Extends DOMNode.

Konstructor

DOMException

DOM operations raise exceptions under particular circumstances, i.e., when an operation is impossible to perform for logical reasons.

See also Rozdział 20.

Właściwości

Tabela 7.

NameTypeRead-onlyDescription
codeintyesAn integer indicating the type of error generated

DOMImplementation

The DOMImplementation interface provides a number of methods for performing operations that are independent of any particular instance of the document object model.

Metody

DOMNode

Metody

Właściwości

Tabela 8.

NameTypeRead-onlyDescription
nodeNamestringyesReturns the more accurate name for the current node type
nodeValuestringnoThe value of this node, depending on its type.
nodeTypeintyesGets the type of the node. One of the predefined XML_xxx_NODE constants
parentNodeDOMNodeyesThe parent of this node.
childNodesDOMNodeListyes A DOMNodeList that contains all children of this node. If there are no children, this is an empty DOMNodeList.
firstChildDOMNodeyes The first child of this node. If there is no such node, this returns NULL.
lastChildDOMNodeyes The last child of this node. If there is no such node, this returns NULL.
previousSiblingDOMNodeyes The node immediately preceding this node. If there is no such node, this returns NULL.
nextSiblingDOMNodeyes The node immediately following this node. If there is no such node, this returns NULL.
attributesDOMNamedNodeMapyes A DOMNamedNodeMap containing the attributes of this node (if it is a DOMElement) or NULL otherwise.
ownerDocumentDOMDocumentyes The DOMDocument object associated with this node.
namespaceURIstringyes The namespace URI of this node, or NULL if it is unspecified.
prefixstringno The namespace prefix of this node, or NULL if it is unspecified.
localNamestringyes Returns the local part of the qualified name of this node.
baseURIstringyes The absolute base URI of this node or NULL if the implementation wasn't able to obtain an absolute URI.
textContentstringno This attribute returns the text content of this node and its descendants.

DOMNodeList

Metody

Właściwości

Tabela 9.

NameTypeRead-onlyDescription
lengthintyes The number of nodes in the list. The range of valid child node indices is 0 to length - 1 inclusive.

DOMNotation

Extends DOMNode

Właściwości

Tabela 10.

NameTypeRead-onlyDescription
publicIdstringyes 
systemIdstringyes 

DOMProcessingInstruction

Extends DOMNode.

Konstructor

Właściwości

Tabela 11.

NameTypeRead-onlyDescription
targetstringyes 
datastringno 

DOMText

Extends DOMCharacterData.

Konstructor

Metody

Właściwości

Tabela 12.

NameTypeRead-onlyDescription
wholeTextstringyes 

DOMXPath

Konstructor

Metody

Właściwości

Tabela 13.

NameTypeRead-onlyDescription
documentDOMDocument  

Przykłady

Many examples in this reference require an XML file. We will use the book.xml that contains the following:

Przykład 1. chapter.xml

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
 "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [
]>
<book id="listing">
 <title>My lists</title>
 <chapter id="books">
  <title>My books</title>
  <para>
   <informaltable>
    <tgroup cols="4">
     <thead>
      <row>
       <entry>Title</entry>
       <entry>Author</entry>
       <entry>Language</entry>
       <entry>ISBN</entry>
      </row>
     </thead>
     <tbody>
      <row>
       <entry>The Grapes of Wrath</entry>
       <entry>John Steinbeck</entry>
       <entry>en</entry>
       <entry>0140186409</entry>
      </row>
      <row>
       <entry>The Pearl</entry>
       <entry>John Steinbeck</entry>
       <entry>en</entry>
       <entry>014017737X</entry>
      </row>
      <row>
       <entry>Samarcande</entry>
       <entry>Amine Maalouf</entry>
       <entry>fr</entry>
       <entry>2253051209</entry>
      </row>
      <!-- TODO: I have a lot of remaining books to add.. -->
     </tbody>
    </tgroup>
   </informaltable>
  </para>
 </chapter>
</book>

Stałe predefinopwane

Poniższe stałe są zdefiniowane w tym rozszerzeniu i stają się dostępne, gdy rozszerzenie jest dokompilowane do PHP, lub załadowane dynamicznie przy starcie.

Tabela 14. XML constants

ConstantValueDescription
XML_ELEMENT_NODE (integer) 1Node is a DOMElement
XML_ATTRIBUTE_NODE (integer) 2Node is a DOMAttr
XML_TEXT_NODE (integer) 3Node is a DOMText
XML_CDATA_SECTION_NODE (integer) 4Node is a DOMCharacterData
XML_ENTITY_REF_NODE (integer) 5Node is a DOMEntityReference
XML_ENTITY_NODE (integer) 6Node is a DOMEntity
XML_PI_NODE (integer) 7Node is a DOMProcessingInstruction
XML_COMMENT_NODE (integer) 8Node is a DOMComment
XML_DOCUMENT_NODE (integer) 9Node is a DOMDocument
XML_DOCUMENT_TYPE_NODE (integer) 10Node is a DOMDocumentType
XML_DOCUMENT_FRAG_NODE (integer) 11Node is a DOMDocumentFragment
XML_NOTATION_NODE (integer) 12Node is a DOMNotation
XML_HTML_DOCUMENT_NODE (integer) 13 
XML_DTD_NODE (integer) 14 
XML_ELEMENT_DECL_NODE (integer) 15 
XML_ATTRIBUTE_DECL_NODE (integer) 16 
XML_ENTITY_DECL_NODE (integer) 17 
XML_NAMESPACE_DECL_NODE (integer) 18 
XML_ATTRIBUTE_CDATA (integer) 1 
XML_ATTRIBUTE_ID (integer) 2 
XML_ATTRIBUTE_IDREF (integer) 3 
XML_ATTRIBUTE_IDREFS (integer) 4 
XML_ATTRIBUTE_ENTITY (integer) 5 
XML_ATTRIBUTE_NMTOKEN (integer) 7 
XML_ATTRIBUTE_NMTOKENS (integer) 8 
XML_ATTRIBUTE_ENUMERATION (integer) 9 
XML_ATTRIBUTE_NOTATION (integer) 10 

Tabela 15. DOMException constants

ConstantValueDescription
DOM_INDEX_SIZE_ERR (integer) 1 If index or size is negative, or greater than the allowed value.
DOMSTRING_SIZE_ERR (integer) 2 If the specified range of text does not fit into a DOMString.
DOM_HIERARCHY_REQUEST_ERR (integer) 3If any node is inserted somewhere it doesn't belong
DOM_WRONG_DOCUMENT_ERR (integer) 4 If a node is used in a different document than the one that created it.
DOM_INVALID_CHARACTER_ERR (integer) 5 If an invalid or illegal character is specified, such as in a name.
DOM_NO_DATA_ALLOWED_ERR (integer) 6 If data is specified for a node which does not support data.
DOM_NO_MODIFICATION_ALLOWED_ERR (integer) 7 If an attempt is made to modify an object where modifications are not allowed.
DOM_NOT_FOUND_ERR (integer) 8 If an attempt is made to reference a node in a context where it does not exist.
DOM_NOT_SUPPORTED_ERR (integer) 9 If the implementation does not support the requested type of object or operation.
DOM_INUSE_ATTRIBUTE_ERR (integer) 10 If an attempt is made to add an attribute that is already in use elsewhere.
DOM_INVALID_STATE_ERR (integer) 11 If an attempt is made to use an object that is not, or is no longer, usable.
DOM_SYNTAX_ERR (integer) 12If an invalid or illegal string is specified.
DOM_INVALID_MODIFICATION_ERR (integer) 13If an attempt is made to modify the type of the underlying object.
DOM_NAMESPACE_ERR (integer) 14 If an attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
DOM_INVALID_ACCESS_ERR (integer) 15 If a parameter or an operation is not supported by the underlying object.
DOM_VALIDATION_ERR (integer) 16 If a call to a method such as insertBefore or removeChild would make the Node invalid with respect to "partial validity", this exception would be raised and the operation would not be done.
Spis treści
DOMAttr->__construct() --  Creates a new DOMAttr object
DOMAttr->isId() --  Checks if attribute is a defined ID
DOMCharacterData->appendData() --  Append the string to the end of the character data of the node
DOMCharacterData->deleteData() --  Remove a range of characters from the node
DOMCharacterData->insertData() --  Insert a string at the specified 16-bit unit offset
DOMCharacterData->replaceData() --  Replace a substring within the DOMCharacterData node
DOMCharacterData->substringData() --  Extracts a range of data from the node
DOMComment->__construct() --  Creates a new DOMComment object
DOMDocument->__construct() --  Creates a new DOMDocument object
DOMDocument->createAttribute() -- Create new attribute
DOMDocument->createAttributeNS() --  Create new attribute node with an associated namespace
DOMDocument->createCDATASection() -- Create new cdata node
DOMDocument->createComment() -- Create new comment node
DOMDocument->createDocumentFragment() -- Create new document fragment
DOMDocument->createElement() -- Create new element node
DOMDocument->createElementNS() --  Create new element node with an associated namespace
DOMDocument->createEntityReference() -- Create new entity reference node
DOMDocument->createProcessingInstruction() -- Creates new PI node
DOMDocument->createTextNode() -- Create new text node
DOMDocument->getElementById() -- Searches for an element with a certain id
DOMDocument->getElementsByTagName() -- Searches for all elements with given tag name
DOMDocument->getElementsByTagNameNS() --  Searches for all elements with given tag name in specified namespace
DOMDocument->importNode() -- Import node into current document
DOMDocument->load() --  Load XML from a file
DOMDocument->loadHTML() --  Load HTML from a string
DOMDocument->loadHTMLFile() --  Load HTML from a file
DOMDocument->loadXML() --  Load XML from a string
DOMDocument->normalize() --  Normalizes the document
DOMDocument->relaxNGValidate() --  Performs relaxNG validation on the document
DOMDocument->relaxNGValidateSource() --  Performs relaxNG validation on the document
DOMDocument->save() --  Dumps the internal XML tree back into a file
DOMDocument->saveHTML() --  Dumps the internal document into a string using HTML formatting
DOMDocument->saveHTMLFile() --  Dumps the internal document into a file using HTML formatting
DOMDocument->saveXML() --  Dumps the internal XML tree back into a string
DOMDocument->schemaValidate() --  Validates a document based on a schema
DOMDocument->schemaValidateSource() --  Validates a document based on a schema
DOMDocument->validate() --  Validates the document based on its DTD
DOMDocument->xinclude() --  Substitutes XIncludes in a DOMDocument Object
DOMElement->__construct() --  Creates a new DOMElement object
DOMElement->getAttribute() -- Returns value of attribute
DOMElement->getAttributeNode() -- Returns attribute node
DOMElement->getAttributeNodeNS() --  Returns attribute node
DOMElement->getAttributeNS() -- Returns value of attribute
DOMElement->getElementsByTagName() -- Gets elements by tagname
DOMElement->getElementsByTagNameNS() -- Get elements by namespaceURI and localName
DOMElement->hasAttribute() -- Checks to see if attribute exists
DOMElement->hasAttributeNS() --  Checks to see if attribute exists
DOMElement->removeAttribute() -- Removes attribute
DOMElement->removeAttributeNode() -- Removes attribute
DOMElement->removeAttributeNS() -- Removes attribute
DOMElement->setAttribute() -- Adds new attribute
DOMElement->setAttributeNode() -- Adds new attribute node to element
DOMElement->setAttributeNodeNS() -- Adds new attribute node to element
DOMElement->setAttributeNS() -- Adds new attribute
DOMAttr->__construct() --  Creates a new DOMEntityReference object
DOMImplementation->createDocument() --  Creates a DOMDocument object of the specified type with its document element
DOMImplementation->createDocumentType() --  Creates an empty DOMDocumentType object
DOMImplementation->hasFeature() --  Test if the DOM implementation implements a specific feature
DOMNamedNodeMap->getNamedItem() --  Retrieves a node specified by name
DOMNamedNodeMap->getNamedItemNS() --  Retrieves a node specified by local name and namespace URI
DOMNamedNodeMap->item() -- Retrieves a node specified by index
DOMNode->appendChild() --  Adds new child at the end of the children
DOMNode->cloneNode() --  Clones a node
DOMNode->hasAttributes() --  Checks if node has attributes
DOMNode->hasChildNodes() --  Checks if node has children
DOMNode->insertBefore() --  Adds a new child before a reference node
DOMNode->isSameNode() --  Indicates if two nodes are the same node
DOMNode->isSupported() --  Checks if feature is supported for specified version
DOMNode->lookupNamespaceURI() --  Gets the namespace URI of the node based on the prefix
DOMNode->lookupPrefix() --  Gets the namespace prefix of the node based on the namespace URI
DOMNode->normalize() --  Normalizes the node
DOMNode->removeChild() --  Removes child from list of children
DOMNode->replaceChild() --  Replaces a child
DOMNodelist->item() --  Retrieves a node specified by index
DOMProcessingInstruction->__construct() --  Creates a new DOMProcessingInstruction object
DOMText->__construct() --  Creates a new DOMText object
DOMText->isWhitespaceInElementContent() --  Indicates whether this text node contains whitespace
DOMText->splitText() --  Breaks this node into two nodes at the specified offset
DOMXPath->__construct() --  Creates a new DOMXPath object
DOMXPath->evaluate() --  Evaluates the given XPath expression and returns a typed result if possible.
DOMXPath->query() --  Evaluates the given XPath expression
DOMXPath->registerNamespace() --  Registers the namespace with the DOMXpath object
dom_import_simplexml --  Gets a DOMElement object from a SimpleXMLElement object



User Contributed Notes

me at somewhere dot com
08-Jan-2006 04:56

When using include/require statements to bring in common PHP code, you may accidentally put a blank line after the trailing "?>" in the included file (ie. two newlines following the "?>" sequence). The output will contain these blank lines and, for outputting HTML, this is no biggie as web browsers are usually relaxed about parsing HTML syntax. However, XML output generated via DOMDocument->saveXML() will be checked more carefully, and XML parsers (like the one that comes with firefox/mozilla) will complain that the <?xml ....?> tag doesn't begin on the first line.

While the best solution is to make sure blank lines do not trail in the included files, you can't always guarentee that a set of include files you grab from the internet will comply. Since the nature of DOM XML generation involves building the DOM structure first, then converting it all to XML, and finally printing it all in one shot, you can safely wipe any blank lines that snuck into the output buffer when files were getting included by calling ob_clear() right before printing/echoing the XML:

dom.php:
<?php
$doc
= new DOMDocument('1.0', 'iso-8859-1');
$doc->formatOutput = true;
?>
...garbage trailing at the end....

foobar.php:
<?php
include 'dom.php'

//Build up $doc here

header("Content-type: text/xml");
ob_clean();
echo
$doc->saveXML();
?>


simlee at indiana dot edu
29-Dec-2005 09:16

The project I'm currently working on uses XPaths to dynamically navigate through chunks of an XML file.  I couldn't find any PHP code on the net that would build the XPath to a node for me, so I wrote my own function.  Turns out it wasn't as hard as I thought it might be (yay recursion), though it does entail using some PHP shenanigans... 

Hopefully it'll save someone else the trouble of reinventing this wheel.

<?php
  
function getNodeXPath( $node ) {
      
// REMEMBER THAT XPATHS USE BASE-1 INSTEAD OF BASE-0!!!
      
       // Get the index for the current node by looping through the siblings.
      
$parentNode = $node->parentNode;
       if(
$parentNode != null ) {
          
$nodeIndex = 0;
           do {
              
$testNode = $parentNode->childNodes->item( $nodeIndex );
              
$nodeName = $testNode->nodeName;
              
$nodeIndex++;
              
              
// PHP trickery!  Here we create a counter based on the node
               //  name of the test node to use in the XPath.
              
if( !isset( $$nodeName ) ) $$nodeName = 1;
               else $
$nodeName++;
              
              
// Failsafe return value.
              
if( $nodeIndex > $parentNode->childNodes->length ) return( "/" );
           } while( !
$node->isSameNode( $testNode ) );
          
          
// Recursively get the XPath for the parent.
          
return( getNodeXPath( $parentNode ) . "/{$node->nodeName}[{$$nodeName}]" );
       } else {
          
// Hit the root node!  Note that the slash is added when
           //  building the XPath, so we return just an empty string.
          
return( "" );
       }
   }
?>


johanwthijs-at-hotmail-dot-com
14-Dec-2005 03:25

Being an experienced ASP developer I was wondering how to replace textual content of a node (with msxml this is simply acheived by setting the 'text' property of a node). Out of frustration I started to play around with SimpleXml but I could not get it to work in combination with xPath.

I took me a lot of time to find out so I hope this helps others:

function replaceNodeText($objXml, $objNode, $strNewContent){
   /*
   This function replaces a node's string content with strNewContent
   */
   $objNodeListNested = &$objNode->childNodes;
   foreach ( $objNodeListNested as $objNodeNested ){
         if ($objNodeNested->nodeType == XML_TEXT_NODE)$objNode->removeChild ($objNodeNested);
   }
  
   $objNode->appendChild($objXml->createTextNode($strNewContent));
}

$objXml= new DOMDocument();
$objXml->loadXML('<root><node id="1">bla</note></root>');
$objXpath = new domxpath($objXml);

$strXpath="/root/node[@id='1']";
$objNodeList = $objXpath ->query($strXpath);
foreach ($objNodeList as $objNode){
//pass the node by reference
replaceNodeText($objXml, &$objNode, $strImportedValue);
}


mark at vectrex dot org dot uk
17-Nov-2005 11:10

Note that these DOM functions expect (and presumably return) all their data in UTF-8 character encoding, regardless of what PHP's current encoding is. This means that text nodes, attribute values etc, should be in utf8.

This applies even if you're generating an XML document which is not ultimately in utf8.

Mark


toby at tobiasly dot com
22-Oct-2005 08:08

This module is not included by default either in the CentOS 4 "centosplus" repository. For those using PHP5 on CentOS 4, a simple "yum --enablerepo=centosplus install php-xml" will do the trick (this will install both the XML and DOM modules).


dariusz <at> grabka <dawt> org
30-Sep-2005 02:46

For those getting the following error with PHP 5.0.x:

Class 'DOMDocument' not found

... please check that the DOM extension has actually been installed.  Based on the documentation, it seems like the DOM extension should be present defacto, but it wasn't part of my FreeBSD mod_php5 build.

The fix in FreeBSD 4.10+:

> cd /usr/ports/lang/php5-extensions
> make config

... select DOM, XML, and anything else your heart desires such as FTP and IMAP..

> make
> make install

> cd /usr/ports/www/mod_php5
> make
> make install

> vi /usr/local/etc/apache/httpd.conf

... remove all instances of *_php4* (otherwise, php5 may not load)

> apachectl restart

enjoy.


pes_cz
29-Sep-2005 09:32

When I tried to parse my XHTML Strict files with DOM extension, it couldn't understand xhtml entities (like &copy;). I found post about it here (14-Jul-2005 09:05) which adviced to add resolveExternals = true, but it was very slow. There was some small note about xml catalogs but without any glue. Here it is:

XML catalogs is something like cache. Download all needed dtd's to /etc/xml, edit file /etc/xml/catalog and add this line: <public publicId="-//W3C//DTD XHTML 1.0 Strict//EN" uri="file:///etc/xml/xhtml1-strict.dtd" />

Thats all. Thanks to http://www.whump.com/moreLikeThis/link/03815


amir.laherATcomplinet.com
22-Aug-2005 02:09

This particular W3C page provides invaluable documentation for the DOM classes implemented in php5 (via libxml2). It fills in plenty of php.net's gaps:

http://www.w3.org/TR/DOM-Level-2-Core/core.html

Some key examples:
* concise summary of the class heirachy (1.1.1)
* clarification that DOM level 2 doesn't allow for population of internal DTDs
* explanation of DOMNode->normalize()
* explanation of the DOMImplementation class

The interfaces are described in OMG's Interface Definition Language


lhaire at lettres dot unige dot ch
18-Aug-2005 06:39

Text nodes contain utf-8 encoding internally, no matter if you specify iso-8859-1 in the file you parse or create a new doc using

$xmltree = new DomDocument('1.0','iso-8859-1');

The solution is to use utf8_decode

$original =
utf8_decode(
$myitem->getElementsByTagName('ORIGINAL')->item(0)->nodeValue);


keyvez at hotmail dot com
12-Aug-2005 09:22

More on the Famous White Space Problem

I was driven to the point of insanity while parsing two XML files. One would parse just fine and the other would give errors.

The preserveWhiteSpace property when set to false would remove white space from one file but not from the other.

So I knew that there was problem with the XML file and not with the parser problem. The XML was well formed so it was not the XML, it had to be something that I couldn't see. It was the white space.

Windows stores a newline as a string of two characters i.e. \r\n and unix stores them as just \n. The preserveWhiteSpace function when set to false would remove all the \n from outside the tags but the \r character still remains if the file is encoded in the windows (wrong) newline format.

So I use the following which adds to the complexity of the program but does the job.

<?php $XMLFileContent = implode('',spliti("\r",implode('',file($Template)))); ?>

Note: This could be improved if efficiency is required.


keyvez at hotmail dot com
09-Aug-2005 04:11

The famous XML white space problem can be easily solved by using the preserveWhiteSpace property of DOMDocument.

example

filename: book.xml

<root>
   <data>
       <name>If Tomorrow Comes...</name>
       <author>Sidney Sheldon</author>
   </data>
</root>

<?php
$DOM
= new DOMDocument();
$DOM->load('book.xml');
echo
$DOM->saveXML();

/* Will output the following

<root>
   <data>
       <name>If Tomorrow Comes...</name>
       <author>Sidney Sheldon</author>
   </data>
</root>

*/

?>

<?php
$DOM
= new DOMDocument();
$DOM->preserveWhiteSpace = false;
$DOM->load('book.xml');
echo
$DOM->saveXML();

/* Will output the following

<root><data><name>If Tomorrow Comes...</name><author>Sidney Sheldon</author></data></root>

*/

?>


aidan at php dot net
19-Jul-2005 03:04

As of PHP 5.1, libxml options may be set using constants rather than the use of proprietary DomDocument properties.

DomDocument->resolveExternals is equivilant to setting
LIBXML_DTDLOAD
LIBXML_DTDATTR

DomDocument->validateOnParse is equivilant to setting
LIBXML_DTDLOAD
LIBXML_DTDVALID

PHP 5.1 users are encouraged to use the new constants.

Example:

DomDocument->load($file, LIBXML_DTDLOAD|LIBXML_DTDATTR);

DomDocument->load($file, LIBXML_DTDLOAD|LIBXML_DTDVALID);


aidan at php dot net
19-Jul-2005 05:27

When dealing with validation or loading, the output errors can be quite annoying.

PHP 5.1 introduces libxml_get_errors().

http://php.net/libxml_get_errors


php at webdevelopers dot cz
14-Jul-2005 09:05

[Editor's Note: If you're using entities, then you have no choice. XML Catalogs can speed DTD resolution.]

Never use

$dom->resolveExternals=true;

when parsing XHTML document that has the DOCTYPE declaration with DTD URL specified in it.

Otherwise parsing the XHTML with DOCTYPE like this one:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

will result in PHP/DOM downloading the DTD file from W3C site when parsing your document. It will add extra delay to your script - I experienced that $dom->load()'s total time was from 1 to 16 seconds.

elixon


mjs15451 at hotmail dot com
10-Jun-2005 05:04

Disregard the last post from toma at smartsemantics dot com.  His function does not parse xml properly.  It does not take into account multiple tags of the same name, multiple of the same attributes in each tag and even attributes that might occur once and not in the others.  So there is no way to accurately tell where you are in the XML structure.  I modified this function and tested it.  If there are any bugs, feel free to modify it and repost but to my knowledge this is an accurate XML parsing function:

function parseXML($dom, &$arrXML){
$arrPointer = &$arrXML;
$dom = $dom->firstChild;
   while (! is_null($dom)){
       switch ($dom->nodeType){
       case XML_TEXT_NODE:
           if (trim($dom->nodeValue) != ''){ //if there are tags inside a tag, generally there isn't a text value so don't show it
           $arrPointer['value'] = $dom->nodeValue;
           }
       break;
       case XML_ELEMENT_NODE:
       $arrPointer = &$arrXML[$dom->nodeName][];
           if ($dom->hasAttributes()){
           $attributes = $dom->attributes;
               foreach ($attributes as $index => $oAttr){
               $arrPointer['attributes'][$oAttr->name] = $oAttr->value;
               }
           }
       break;
       }
       if ($dom->hasChildNodes()){
       parseXML($dom, $arrPointer);
       }
   $dom = $dom->nextSibling;
   }
}


toma at smartsemantics dot com
12-May-2005 04:22

Based loosely on some previously provided examples, here are 2 functions to read in an XML file using DOM and parse it into a usable array for reading/access.  The output is a very clean array.

------------------------------------
function LoadXMLFile( $path,$file) {
   $oldpath= getcwd( ) ;
   chdir( $path) ;
   $dom= DOMDocument::load( $path.$file) ;
   chdir( $oldpath) ;
   $xml= array( ) ;
   parseXML( $dom,$xml) ;
   return $xml;
}

function parseXML( $dom,&$arr)
{
   $arr_ptr= &$arr;
   $dom= $dom->firstChild;
   while( !is_null( $dom) ) {
       switch ( $dom->nodeType) {
           case XML_TEXT_NODE:
               if( trim( $dom->nodeValue)  != '') {
                   if( is_array( $arr_ptr) ) {
                       $arr_ptr[]= $dom->nodeValue;
                   }
                   elseif( $arr_ptr != '') {
                       $arr_ptr= array( $arr_ptr) ;
                       $arr_ptr[]= $dom->nodeValue;
                   }
                   else{
                       $arr_ptr= $dom->nodeValue;
                   }
               }
               break;
           case XML_ELEMENT_NODE:
               $arr_ptr= &$arr[$dom->nodeName];
               if ( $dom->hasAttributes( )  ) {
                   $attributes= $dom->attributes;
                   foreach( $attributes as $index= >$domobj) {
                       $arr_ptr['ATTRIBUTES'] [$domobj->name]= $domobj->value;
                   }
               }
               break;
       }
       if( $dom->hasChildNodes( ) ) {
           parseXML( $dom,$arr_ptr) ;
       }
       $dom= $dom->nextSibling;
   }
}
------------------------------------

Example usage:
$CONFIG= LoadXMLFile( '/var/www/html/site.com/php-bin/','config.xml') ;


sofa77 at gmx dot de
02-May-2005 05:34

please take account, that the following - usually working check - won't work as expected. i was looking for a too long time for an error, where no error were.

<?php
$last
= null;

$dom = new DOMDocument;
$first = $dom->createElement('firstEl');
$last = $dom->appendChild($first);
$second = $dom->createElement('secondEl');
$last = $last->appendChild($second);

echo
htmlspecialchars($dom->saveXML());
echo
$last->parentNode->nodeName;

if(!isset(
$last->parentNode->nodeName))
{
 
trigger_error('WRONG NOTICE: no parent', E_USER_NOTICE);
}
if(!
$last->parentNode)
{
 
trigger_error('no parent', E_USER_NOTICE);
}
if(!
$last->parentNode->nodeName)
{
 
trigger_error('no parent', E_USER_NOTICE);
}
?>


spammable69 at hotmail dot com
12-Apr-2005 07:18

I wrote a framework to implement the StyleSheet interfaces as specified on the W3C website.  The code is written in PHP, and is NOT a complete implementation.  Use it how ya like.  I was planning on adding the CSSStyleSheet interfaces as well.  Feel free to ask.

<?
  
class StyleSheetList {
      
public length;
      
private self;
      
       function
__construct ( ) {
          
$this->self = array();
       }
      
       function
__get($property, $&ret) {
           if(
$property == 'length')
              
$ret = count($this->self);
           return
true;
       }
      
       function
__set($property, $val) {
           if(
$property == 'length')
               return
true;
       }
      
       function
item( $index ) {
           return
$this->self[$index];
       }
   }
  
   class
MediaList extends StyleSheetList {
      
       function
appendMedium ( $newMedium ) {
          
array_push($this->self, $newMedium);
       }
      
       function
deleteMedium ( $oldMedium ) {
           foreach(
$this->self as $item) {
               if(
$item == $oldMedium ) {
                  
$item = $this->self[ $this->length-1 ];
                  
array_pop($this->self);
                   break;
               }
           }
       }
   }
  
   class
DocumentStyle {
      
public styleSheets;
      
       function
__construct ( ) {
          
$this->styleSheets = new StyleSheetList();
       }
      
       function
__set($property, $val) {
           if(
$property == 'styleSheets')
               return
true;
       }
   }
  
   class
LinkStyle {
      
public sheet;
      
       function
__construct () {
          
$this->sheet = new StyleSheet();
       }
      
       function
__set($property, $val) {
           if(
$property == 'sheet')
               return
true;
       }
   }
  
   class
StyleSheet {
      
public type;
      
public disabled;
      
public ownerNode;
      
public parentStyleSheet;
      
public href;
      
public title;
      
public media;
      
       function
__construct( $type, $disabled, $ownerNode, $parentStyleSheet, $href, $title){
          
$this->type = $type;
          
$this->disabled = $disabled;
          
$this->media = new MediaList();
          
$this->ownerNode = $ownerNode;
          
$this->parentStyleSheet = $parentStyleSheet;
          
$this->href = $href;
          
$this->title = $title;
       }
   }
?>

Only contactable via http://murpsoft.com/contact.html


shruti224 at yahoo dot com
31-Mar-2005 05:25

my bad..its not an attribute ..its a text element node so if one checks for that, it can be easily corrected.

Wrote the corrected function. I hope it takes care of all conditions. If someone finds any issues with it, I would be glad to know about it.

function readXml($domnode, &$array)
{
       $array_ptr = &$array;
       $domnode = $domnode->firstChild;
       while (!is_null($domnode))
       {
               switch ($domnode->nodeType)
               {
                       case XML_TEXT_NODE:      if(!(trim($domnode->nodeValue) == ""))
                                                       $array_ptr['cdata'] = $domnode->nodeValue;
                                               break;
                       case XML_ELEMENT_NODE:
                                               $array_ptr = &$array[$domnode->nodeName][];
                                               if ($domnode->hasAttributes() )
                                               {
                                                       $attributes = $domnode->attributes;
                                                       foreach ($attributes as $index => $domobj)
                                                       {
                                                             $array_ptr[$domobj->name] = $domobj->value;
                                                       }
                                               }
                                               break;
               }
               if ( $domnode->hasChildNodes() )
               {
                       readXml($domnode, $array_ptr);
               }
               $domnode = $domnode->nextSibling;
       }
}


roger4a45 at yahoo dot es
23-Feb-2005 12:35

Output a special chars like &agrave in your XML/XSLT

Following example create a easy XML that is transformed by a xsl i'll show below:

XSL FILE v1.xsl

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/TR/REC-html40">
<xsl:template match="TEST">
<xsl:value-of select="NAME"/><BR/>
</xsl:template>
</xsl:stylesheet>

PHP SOURCE CODE

<?php
$xml
= new DOMDocument('1.0', 'utf-8'); //Change encoding to utf-8
$xsl = new DOMDocument;
$xsl->load('v1.xsl');
$proc = new XSLTProcessor;
$root = $xml->CreateElement("TEST");
$val = utf8_encode ('Andr

 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt