/** * Functions / Methods */ function xml_read($xmlSource, $from_file = TRUE) { global $xpath; $xpath = NULL; $xmlOptions = array(XML_OPTION_CASE_FOLDING => FALSE, XML_OPTION_SKIP_WHITE => TRUE); $xpath = new XPath(FALSE, $xmlOptions); //$xpath->bDebugXmlParse = TRUE; if ($from_file) { if (file_exists($xmlSource) && is_readable($xmlSource)) { if (!$xpath->importFromFile($xmlSource)) { die('xml_read(): XPath error > ' . $xpath->getLastError()); } } else { die('xml_read(): Can\'t find or open: ' . realpath($xmlSource)); } } else { if (!empty($xmlSource)) { if (!$xpath->importFromString($xmlSource)) { die('xml_read(): XPath error > ' . $xpath->getLastError()); } } else { die('xml_read(): Given source is empty in line.'); } } return true; }
$q = '?'; $xmlSource = <<<EOD <{$q}Process_Instruction test="© All right reserved" {$q}> <AAA foo="bar"> ,,1,, ..1.. <![CDATA[ bla bla newLine blo blo ]]> <BBB foo="bar"> ..2.. </BBB>..3..<CC/> ..4..</AAA> EOD; // The sample code: $xmlOptions = array(XML_OPTION_CASE_FOLDING => TRUE, XML_OPTION_SKIP_WHITE => TRUE); $xPath = new XPath(FALSE, $xmlOptions); //$xPath->bDebugXmlParse = TRUE; if (!$xPath->importFromString($xmlSource)) { echo $xPath->getLastError(); exit; } _title("Following was imported:"); echo $xPath->exportAsHtml(); _title("Get some content"); echo "Last text part in <AAA>: '" . $xPath->wholeText('/AAA[1]', -1) . "'<br>\n"; echo "All the text in <AAA>: '" . $xPath->wholeText('/AAA[1]') . "'<br>\n"; echo "The attibute value in <BBB> using getAttributes('/AAA[1]/BBB[1]', 'FOO'): '" . $xPath->getAttributes('/AAA[1]', 'FOO') . "'<br>\n"; echo "The attibute value in <BBB> using getData('/AAA[1]/@FOO'): '" . $xPath->getData('/AAA[1]/@FOO') . "'<br>\n"; _title("Append some additional XML below /AAA/BBB:"); $xPath->appendChild('/AAA[1]/BBB[1]', '<CCC> Step 1. Append new node </CCC>', $afterText = FALSE); $xPath->appendChild('/AAA[1]/BBB[1]', '<CCC> Step 2. Append new node </CCC>', $afterText = TRUE); $xPath->appendChild('/AAA[1]/BBB[1]', '<CCC> Step 3. Append new node </CCC>', $afterText = TRUE); echo $xPath->exportAsHtml(); _title("Insert some additional XML below <AAA>:");