PHP Doku:: Gets content of node - function.domnode-get-content.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzXML-ManipulationDOM-XMLDOM-XML-FunktionenDomNode->get_content

Ein Service von Reinhard Neidl - Webprogrammierung.

DOM-XML-Funktionen

<<DomNode->first_child

DomNode->has_attributes>>

DomNode->get_content

(PHP 4 >= 4.2.0)

DomNode->get_content Gets content of node

Beschreibung

string DomNode->get_content ( void )

This function returns the content of the actual node.

Beispiel #1 Getting a content

<?php
if (!$dom domxml_open_mem($xmlstr)) {
  echo 
"Error while parsing the document\n";
  exit;
}

$root $dom->document_element();

$node_array $root->get_elements_by_tagname("element");

for (
$i 0$i<count($node_array); $i++) {
    
$node $node_array[$i];
    echo 
"The element[$i] is: " $node->get_content();
}

?>


5 BenutzerBeiträge:
- Beiträge aktualisieren...
kris {at} linked {dot} com {dot} mt
2.09.2010 8:04
To print the node simply create a new document import the node and save it to html or whatever you need

e.g.:

<?php
$temp
= new DOMDocument('1.0', 'UTF-8');
$temp_node = $temp->importNode($mynode, TRUE);
$temp->appendChild($temp_node);
$temp->saveHTML();
?>
d dot cicognani at ciconet dot it
24.12.2005 8:43
Only to correct a very small error, but maybe difficult to find in che function GetContentAsString posted by someone in April 2005.
There was $anode-node_value() instead of $anode->node_value().
This is the right version:

<?php

function GetContentAsString($node) {   
 
$st = "";
  foreach (
$node->child_nodes() as $cnode)
   if (
$cnode->node_type()==XML_TEXT_NODE)
    
$st .= $cnode->node_value();
   else if (
$cnode->node_type()==XML_ELEMENT_NODE) {
    
$st .= "<" . $cnode->node_name();
     if (
$attribnodes=$cnode->attributes()) {
      
$st .= " ";
       foreach (
$attribnodes as $anode)
        
$st .= $anode->node_name() . "='" .
          
$anode->node_value() . "'";
       }   
    
$nodeText = GetContentAsString($cnode);
     if (empty(
$nodeText) && !$attribnodes)
      
$st .= " />";        // unary
    
else
      
$st .= ">" . $nodeText . "</" .
        
$cnode->node_name() . ">";
     }
  return
$st;
  }

?>
krautman at gmx dot net
6.12.2005 16:14
get_content() always returns UTF-8 (as should be!).
To get the content right just code it like

$myContent = utf8_decode($myNode->get_content());

Works fine!
sysadmin at webnow dot com dot br
26.07.2005 14:43
Seems that get_content() always returns utf-8.
Opening document with something like

$dom->dump_mem(true,"ISO-8859-1")

makes no difference!

11.04.2005 18:41
Here's a routine I wrote that acts like get_content() but returns embedded XHTML in the string returned.  I needed this as I was wanting to embed HTML formatting codes in my HTML (e.g., "<br />") and converting them to HTML entities or using CDATA was a huge hassle.

<?php

function GetContentAsString($node) {   
 
$st = "";
  foreach (
$node->child_nodes() as $cnode)
    if (
$cnode->node_type()==XML_TEXT_NODE)
     
$st .= $cnode->node_value();
    else if (
$cnode->node_type()==XML_ELEMENT_NODE) {
     
$st .= "<" . $cnode->node_name();
      if (
$attribnodes=$cnode->attributes()) {
       
$st .= " ";
        foreach (
$attribnodes as $anode)
         
$st .= $anode->node_name() . "='" .
           
$anode-node_value() . "'";
        }   
     
$nodeText = GetContentAsString($cnode);
      if (empty(
$nodeText) && !$attribnodes)
       
$st .= " />";        // unary
     
else
       
$st .= ">" . $nodeText . "</" .
         
$cnode->node_name() . ">";
      }
  return
$st;
  }

?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)