예제 #1
0
 * creating an end element
 */
print "creating an end element:<br>";
print htmlentities(XML_Util::createEndElement("myNs:myTag"));
print "\n<br><br>\n";
/**
 * creating a CData section
 */
print "creating a CData section:<br>";
print htmlentities(XML_Util::createCDataSection("I am content."));
print "\n<br><br>\n";
/**
 * creating a comment
 */
print "creating a comment:<br>";
print htmlentities(XML_Util::createComment("I am a comment."));
print "\n<br><br>\n";
/**
 * creating an XML tag with multiline mode
 */
$tag = array("qname" => "foo:bar", "namespaceUri" => "http://foo.com", "attributes" => array("key" => "value", "argh" => "fruit&vegetable"), "content" => "I'm inside the tag & contain dangerous chars");
print "creating a tag with qualified name and namespaceUri:<br>\n";
print "<pre>";
print htmlentities(XML_Util::createTagFromArray($tag, XML_UTIL_REPLACE_ENTITIES, true));
print "</pre>";
print "\n<br><br>\n";
/**
 * create an attribute string without replacing the entities
 */
$atts = array('series' => 'Starsky &amp; Hutch', 'channel' => 'ABC');
print "creating a attribute string, entities in values already had been replaced:<br>";
예제 #2
0
 /**
  * serialize an array
  *
  * @param array  &$array     array to serialize
  * @param string $tagName    name of the root tag
  * @param array  $attributes attributes for the root tag
  *
  * @return string $string serialized data
  * @access private
  * @uses XML_Util::isValidName() to check, whether key has to be substituted
  * @uses XML_Util::replaceEntities()
  * @uses XML_Util::createComment()
  * @uses PEAR::popExpect()
  * @uses PEAR::expectError()
  */
 function _serializeArray(&$array, $tagName = null, $attributes = array())
 {
     $_content = null;
     $_comment = null;
     // check for comment
     if ($this->options[XML_SERIALIZER_OPTION_COMMENT_KEY] !== null) {
         if (isset($array[$this->options[XML_SERIALIZER_OPTION_COMMENT_KEY]])) {
             $_comment = $array[$this->options[XML_SERIALIZER_OPTION_COMMENT_KEY]];
             unset($array[$this->options[XML_SERIALIZER_OPTION_COMMENT_KEY]]);
         }
     }
     /**
      * check for special attributes
      */
     if ($this->options[XML_SERIALIZER_OPTION_ATTRIBUTES_KEY] !== null) {
         if (isset($array[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTES_KEY]])) {
             $attributes = $array[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTES_KEY]];
             unset($array[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTES_KEY]]);
         }
         /**
          * check for special content
          */
         if ($this->options[XML_SERIALIZER_OPTION_CONTENT_KEY] !== null) {
             if (isset($array[$this->options[XML_SERIALIZER_OPTION_CONTENT_KEY]])) {
                 $_content = XML_Util::replaceEntities($array[$this->options[XML_SERIALIZER_OPTION_CONTENT_KEY]]);
                 unset($array[$this->options[XML_SERIALIZER_OPTION_CONTENT_KEY]]);
             }
         }
     }
     if ($this->options[XML_SERIALIZER_OPTION_IGNORE_NULL] === true) {
         foreach (array_keys($array) as $key) {
             if (is_null($array[$key])) {
                 unset($array[$key]);
             }
         }
     }
     /*
      * if mode is set to simpleXML, check whether
      * the array is associative or indexed
      */
     if (is_array($array) && !empty($array) && $this->options[XML_SERIALIZER_OPTION_MODE] == XML_SERIALIZER_MODE_SIMPLEXML) {
         $indexed = true;
         foreach ($array as $key => $val) {
             if (!is_int($key)) {
                 $indexed = false;
                 break;
             }
         }
         if ($indexed && $this->options[XML_SERIALIZER_OPTION_MODE] == XML_SERIALIZER_MODE_SIMPLEXML) {
             $string = '';
             foreach ($array as $key => $val) {
                 $string .= $this->_serializeValue($val, $tagName, $attributes);
                 $string .= $this->options[XML_SERIALIZER_OPTION_LINEBREAKS];
                 // do indentation
                 if ($this->options[XML_SERIALIZER_OPTION_INDENT] !== null && $this->_tagDepth > 0) {
                     $string .= str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth);
                 }
             }
             return rtrim($string);
         }
     }
     $scalarAsAttributes = false;
     if (is_array($this->options[XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES]) && isset($this->options[XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES][$tagName])) {
         $scalarAsAttributes = $this->options[XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES][$tagName];
     } elseif ($this->options[XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES] === true) {
         $scalarAsAttributes = true;
     }
     if ($scalarAsAttributes === true) {
         $this->expectError('*');
         foreach ($array as $key => $value) {
             if (is_scalar($value) && XML_Util::isValidName($key) === true) {
                 unset($array[$key]);
                 $attributes[$this->options[XML_SERIALIZER_OPTION_PREPEND_ATTRIBUTES] . $key] = $value;
             }
         }
         $this->popExpect();
     } elseif (is_array($scalarAsAttributes)) {
         $this->expectError('*');
         foreach ($scalarAsAttributes as $key) {
             if (!isset($array[$key])) {
                 continue;
             }
             $value = $array[$key];
             if (is_scalar($value) && XML_Util::isValidName($key) === true) {
                 unset($array[$key]);
                 $attributes[$this->options[XML_SERIALIZER_OPTION_PREPEND_ATTRIBUTES] . $key] = $value;
             }
         }
         $this->popExpect();
     }
     // check for empty array => create empty tag
     if (empty($array)) {
         $tag = array('qname' => $tagName, 'content' => $_content, 'attributes' => $attributes);
     } else {
         $this->_tagDepth++;
         $tmp = $_content . $this->options[XML_SERIALIZER_OPTION_LINEBREAKS];
         foreach ($array as $key => $value) {
             // do indentation
             if ($this->options[XML_SERIALIZER_OPTION_INDENT] !== null && $this->_tagDepth > 0) {
                 $tmp .= str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth);
             }
             // copy key
             $origKey = $key;
             $this->expectError('*');
             // key cannot be used as tagname => use default tag
             $valid = XML_Util::isValidName($key);
             $this->popExpect();
             if (PEAR::isError($valid)) {
                 if ($this->options[XML_SERIALIZER_OPTION_CLASSNAME_AS_TAGNAME] && is_object($value)) {
                     $key = get_class($value);
                 } else {
                     $key = $this->_getDefaultTagname($tagName);
                 }
             }
             // once we've established the true $key, is there a tagmap for it?
             if (isset($this->options[XML_SERIALIZER_OPTION_TAGMAP][$key])) {
                 $key = $this->options[XML_SERIALIZER_OPTION_TAGMAP][$key];
             }
             $atts = array();
             if ($this->options[XML_SERIALIZER_OPTION_TYPEHINTS] === true) {
                 $atts[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTE_TYPE]] = gettype($value);
                 if ($key !== $origKey) {
                     $atts[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTE_KEY]] = (string) $origKey;
                 }
             }
             $tmp .= $this->_createXMLTag(array('qname' => $key, 'attributes' => $atts, 'content' => $value));
             $tmp .= $this->options[XML_SERIALIZER_OPTION_LINEBREAKS];
         }
         $this->_tagDepth--;
         if ($this->options[XML_SERIALIZER_OPTION_INDENT] !== null && $this->_tagDepth > 0) {
             $tmp .= str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth);
         }
         if (trim($tmp) === '') {
             $tmp = null;
         }
         $tag = array('qname' => $tagName, 'content' => $tmp, 'attributes' => $attributes);
     }
     if ($this->options[XML_SERIALIZER_OPTION_TYPEHINTS] === true) {
         if (!isset($tag['attributes'][$this->options[XML_SERIALIZER_OPTION_ATTRIBUTE_TYPE]])) {
             $tag['attributes'][$this->options[XML_SERIALIZER_OPTION_ATTRIBUTE_TYPE]] = 'array';
         }
     }
     $string = '';
     if (!is_null($_comment)) {
         $string .= XML_Util::createComment($_comment);
         $string .= $this->options[XML_SERIALIZER_OPTION_LINEBREAKS];
         if ($this->options[XML_SERIALIZER_OPTION_INDENT] !== null && $this->_tagDepth > 0) {
             $string .= str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth);
         }
     }
     $string .= $this->_createXMLTag($tag, false);
     return $string;
 }
예제 #3
0
 * creating an end element
 */
print 'creating an end element:<br>';
print htmlentities(XML_Util::createEndElement('myNs:myTag'));
print "\n<br><br>\n";
/**
 * creating a CData section
 */
print 'creating a CData section:<br>';
print htmlentities(XML_Util::createCDataSection('I am content.'));
print "\n<br><br>\n";
/**
 * creating a comment
 */
print 'creating a comment:<br>';
print htmlentities(XML_Util::createComment('I am a comment.'));
print "\n<br><br>\n";
/**
 * creating an XML tag with multiline mode
 */
$tag = array('qname' => 'foo:bar', 'namespaceUri' => 'http://foo.com', 'attributes' => array('key' => 'value', 'argh' => 'fruit&vegetable'), 'content' => 'I\'m inside the tag & contain dangerous chars');
print 'creating a tag with qualified name and namespaceUri:<br>';
print '<pre>';
print htmlentities(XML_Util::createTagFromArray($tag, XML_UTIL_REPLACE_ENTITIES, true));
print '</pre>';
print "\n<br><br>\n";
/**
 * create an attribute string without replacing the entities
 */
$atts = array('series' => 'Starsky &amp; Hutch', 'channel' => 'ABC');
print 'creating a attribute string, ' . 'entities in values already had been replaced:<br>';