/** * create a tag * * This method will call XML_Util::createTagFromArray(), which * is more flexible. * * <code> * require_once 'XML/Util.php'; * * // create an XML tag: * $tag = XML_Util::createTag("myNs:myTag", array("foo" => "bar"), "This is inside the tag", "http://www.w3c.org/myNs#"); * </code> * * @access public * @static * @param string $qname qualified tagname (including namespace) * @param array $attributes array containg attributes * @param mixed $content * @param string $namespaceUri URI of the namespace * @param integer $replaceEntities whether to replace XML special chars in content, embedd it in a CData section or none of both * @param boolean $multiline whether to create a multiline tag where each attribute gets written to a single line * @param string $indent string used to indent attributes (_auto indents attributes so they start at the same column) * @param string $linebreak string used for linebreaks * @param string $encoding encoding that should be used to translate content * @return string $string XML tag * @see XML_Util::createTagFromArray() * @uses XML_Util::createTagFromArray() to create the tag */ function createTag($qname, $attributes = array(), $content = null, $namespaceUri = null, $replaceEntities = XML_UTIL_REPLACE_ENTITIES, $multiline = false, $indent = "_auto", $linebreak = "\n", $encoding = XML_UTIL_ENTITIES_XML) { $tag = array("qname" => $qname, "attributes" => $attributes); // add tag content if ($content !== null) { $tag["content"] = $content; } // add namespace Uri if ($namespaceUri !== null) { $tag["namespaceUri"] = $namespaceUri; } return XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $linebreak, $encoding); }
/** * creating an XML tag with a CData Section */ $tag = array("qname" => "foo", "attributes" => array("key" => "value", "argh" => "tütü"), "content" => "Also XHTML-tags can be created and HTML entities can be replaced Ä ä Ü ö <>."); print "creating a tag with HTML entities:<br>\n"; print htmlentities(XML_Util::createTagFromArray($tag, XML_UTIL_ENTITIES_HTML)); print "\n<br><br>\n"; /** * creating an XML tag with createTag */ print "creating a tag with createTag:<br>"; print htmlentities(XML_Util::createTag("myNs:myTag", array("foo" => "bar"), "This is inside the tag", "http://www.w3c.org/myNs#")); print "\n<br><br>\n"; /** * trying to create an XML tag with an array as content */ $tag = array("qname" => "bar", "content" => array("foo" => "bar")); print "trying to create an XML tag with an array as content:<br>\n"; print "<pre>"; print_r(XML_Util::createTagFromArray($tag)); print "</pre>"; print "\n<br><br>\n"; /** * trying to create an XML tag without a name */ $tag = array("attributes" => array("foo" => "bar")); print "trying to create an XML tag without a name:<br>\n"; print "<pre>"; print_r(XML_Util::createTagFromArray($tag)); print "</pre>"; print "\n<br><br>\n";
/** * create a tag from an array * this method awaits an array in the following format * array( * 'qname' => $tagName, * 'attributes' => array(), * 'content' => $content, // optional * 'namespace' => $namespace // optional * 'namespaceUri' => $namespaceUri // optional * ) * * @access private * @param array $tag tag definition * @param boolean $replaceEntities whether to replace XML entities in content or not * @return string $string XML tag */ function _createXMLTag($tag, $replaceEntities = true) { if ($this->options['indentAttributes'] !== false) { $multiline = true; $indent = str_repeat($this->options['indent'], $this->_tagDepth); if ($this->options['indentAttributes'] == '_auto') { $indent .= str_repeat(' ', strlen($tag['qname']) + 2); } else { $indent .= $this->options['indentAttributes']; } } else { $indent = $multiline = false; } if (is_array($tag['content'])) { if (empty($tag['content'])) { $tag['content'] = ''; } } elseif (is_scalar($tag['content']) && (string) $tag['content'] == '') { $tag['content'] = ''; } if (is_scalar($tag['content']) || is_null($tag['content'])) { if ($this->options['encoding'] == 'UTF-8' && version_compare(phpversion(), '5.0.0', 'lt')) { $tag['content'] = utf8_encode($tag['content']); } if ($replaceEntities === true) { $replaceEntities = XML_UTIL_ENTITIES_XML; } $tag = XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $this->options['linebreak']); } elseif (is_array($tag['content'])) { $tag = $this->_serializeArray($tag['content'], $tag['qname'], $tag['attributes']); } elseif (is_object($tag['content'])) { $tag = $this->_serializeObject($tag['content'], $tag['qname'], $tag['attributes']); } elseif (is_resource($tag['content'])) { settype($tag['content'], 'string'); $tag = XML_Util::createTagFromArray($tag, $replaceEntities); } return $tag; }
/** * create a tag * * This method will call XML_Util::createTagFromArray(), which * is more flexible. * * <code> * require_once 'XML/Util.php'; * * // create an XML tag: * $tag = XML_Util::createTag('myNs:myTag', * array('foo' => 'bar'), * 'This is inside the tag', * 'http://www.w3c.org/myNs#'); * </code> * * @param string $qname qualified tagname (including namespace) * @param array $attributes array containg attributes * @param mixed $content the content * @param string $namespaceUri URI of the namespace * @param int $replaceEntities whether to replace XML special chars in * content, embedd it in a CData section * or none of both * @param bool $multiline whether to create a multiline tag where * each attribute gets written to a single line * @param string $indent string used to indent attributes * (_auto indents attributes so they start * at the same column) * @param string $linebreak string used for linebreaks * @param bool $sortAttributes Whether to sort the attributes or not * * @return string XML tag * @access public * @static * @see createTagFromArray() * @uses createTagFromArray() to create the tag */ function createTag($qname, $attributes = array(), $content = null, $namespaceUri = null, $replaceEntities = XML_UTIL_REPLACE_ENTITIES, $multiline = false, $indent = '_auto', $linebreak = "\n", $sortAttributes = true) { $tag = array('qname' => $qname, 'attributes' => $attributes); // add tag content if ($content !== null) { $tag['content'] = $content; } // add namespace Uri if ($namespaceUri !== null) { $tag['namespaceUri'] = $namespaceUri; } return XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $linebreak, $sortAttributes); }
/** * create a tag from an array * this method awaits an array in the following format * array( * 'qname' => $tagName, * 'attributes' => array(), * 'content' => $content, // optional * 'namespace' => $namespace // optional * 'namespaceUri' => $namespaceUri // optional * ) * * @param array $tag tag definition * @param boolean $firstCall whether or not this is the first call * * @return string $string XML tag * @access private */ function _createXMLTag($tag, $firstCall = true) { // build fully qualified tag name if ($this->options[XML_SERIALIZER_OPTION_NAMESPACE] !== null) { if (is_array($this->options[XML_SERIALIZER_OPTION_NAMESPACE])) { $tag['qname'] = $this->options[XML_SERIALIZER_OPTION_NAMESPACE][0] . ':' . $tag['qname']; } else { $tag['qname'] = $this->options[XML_SERIALIZER_OPTION_NAMESPACE] . ':' . $tag['qname']; } } // attribute indentation if ($this->options[XML_SERIALIZER_OPTION_INDENT_ATTRIBUTES] !== false) { $multiline = true; $indent = str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth); if ($this->options[XML_SERIALIZER_OPTION_INDENT_ATTRIBUTES] == '_auto') { $indent .= str_repeat(' ', strlen($tag['qname']) + 2); } else { $indent .= $this->options[XML_SERIALIZER_OPTION_INDENT_ATTRIBUTES]; } } else { $multiline = false; $indent = false; } if (is_array($tag['content'])) { if (empty($tag['content'])) { $tag['content'] = ''; } } elseif (XML_SERIALIZER_OPTION_FALSE_AS_STRING && $tag['content'] === false) { $tag['content'] = '0'; } elseif (is_scalar($tag['content']) && (string) $tag['content'] == '') { $tag['content'] = ''; } // replace XML entities if ($firstCall === true) { if ($this->options[XML_SERIALIZER_OPTION_CDATA_SECTIONS] === true) { $replaceEntities = XML_UTIL_CDATA_SECTION; } else { $replaceEntities = $this->options[XML_SERIALIZER_OPTION_ENTITIES]; } } else { // this is a nested call, so value is already encoded // and must not be encoded again $replaceEntities = XML_SERIALIZER_ENTITIES_NONE; // but attributes need to be encoded anyways // (done here because the rest of the code assumes the same encoding // can be used both for attributes and content) foreach ($tag['attributes'] as $k => $v) { $v = XML_Util::replaceEntities($v, $this->options[XML_SERIALIZER_OPTION_ENTITIES]); $tag['attributes'][$k] = $v; } } if (is_scalar($tag['content']) || is_null($tag['content'])) { if ($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC]) { if ($firstCall === true) { $tag['content'] = call_user_func($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC], $tag['content']); } $tag['attributes'] = array_map($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC], $tag['attributes']); } $tag = XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $this->options[XML_SERIALIZER_OPTION_LINEBREAKS]); } elseif (is_array($tag['content'])) { $tag = $this->_serializeArray($tag['content'], $tag['qname'], $tag['attributes']); } elseif (is_object($tag['content'])) { $tag = $this->_serializeObject($tag['content'], $tag['qname'], $tag['attributes']); } elseif (is_resource($tag['content'])) { settype($tag['content'], 'string'); if ($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC]) { if ($replaceEntities === true) { $tag['content'] = call_user_func($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC], $tag['content']); } $tag['attributes'] = array_map($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC], $tag['attributes']); } $tag = XML_Util::createTagFromArray($tag, $replaceEntities); } return $tag; }
/** * Print a group of same tag in the XML report. * * Groups list are : extension(s), constant(s), token(s) * * @param array $dataSrc Data source * @param string $tagName Name of the XML tag * * @return string * @access private * @since version 1.7.0b4 (2008-04-03) */ function _printTagList($dataSrc, $tagName) { $msg = ''; if ($tagName == 'function') { $c = 0; foreach ($dataSrc as $version => $functions) { $c += count($functions); } $attributes = array('count' => $c); } elseif ($tagName == 'condition') { if ($this->_parser->options['debug'] === true) { $c = 0; foreach ($dataSrc[1] as $cond => $elements) { $c += count($elements); } $attributes = array('count' => $c, 'level' => $dataSrc[0]); } else { $attributes = array('level' => $dataSrc[0]); } } else { $attributes = array('count' => count($dataSrc)); } $msg .= XML_Util::createStartElement($tagName . 's', $attributes); $msg .= PHP_EOL; if ($tagName == 'function') { foreach ($dataSrc as $version => $functions) { foreach ($functions as $data) { $attr = array('version' => $version); if (!empty($data['extension'])) { $attr['extension'] = $data['extension']; $attr['pecl'] = $data['pecl'] === true ? 'true' : 'false'; } $tag = array('qname' => $tagName, 'attributes' => $attr, 'content' => $data['function']); $msg .= XML_Util::createTagFromArray($tag); $msg .= PHP_EOL; } } } elseif ($tagName == 'condition') { if ($this->_parser->options['debug'] == true) { foreach ($dataSrc[1] as $cond => $elements) { $cond = $cond == 0 ? 1 : $cond * 2; foreach ($elements as $data) { $tag = array('qname' => $tagName, 'attributes' => array('level' => $cond), 'content' => $data); $msg .= XML_Util::createTagFromArray($tag); $msg .= PHP_EOL; } } } } else { foreach ($dataSrc as $data) { $tag = array('qname' => $tagName, 'attributes' => array(), 'content' => $data); $msg .= XML_Util::createTagFromArray($tag); $msg .= PHP_EOL; } } $msg .= XML_Util::createEndElement($tagName . 's'); $msg .= PHP_EOL; return $msg; }
/** * create a tag from an array * this method awaits an array in the following format * array( * 'qname' => $tagName, * 'attributes' => array(), * 'content' => $content, // optional * 'namespace' => $namespace // optional * 'namespaceUri' => $namespaceUri // optional * ) * * @access private * @param array $tag tag definition * @param boolean $replaceEntities whether to replace XML entities in content or not * @return string $string XML tag */ function _createXMLTag($tag, $firstCall = true) { // build fully qualified tag name if ($this->options[XML_SERIALIZER_OPTION_NAMESPACE] !== null) { if (is_array($this->options[XML_SERIALIZER_OPTION_NAMESPACE])) { $tag['qname'] = $this->options[XML_SERIALIZER_OPTION_NAMESPACE][0] . ':' . $tag['qname']; } else { $tag['qname'] = $this->options[XML_SERIALIZER_OPTION_NAMESPACE] . ':' . $tag['qname']; } } // attribute indentation if ($this->options[XML_SERIALIZER_OPTION_INDENT_ATTRIBUTES] !== false) { $multiline = true; $indent = str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth); if ($this->options[XML_SERIALIZER_OPTION_INDENT_ATTRIBUTES] == '_auto') { $indent .= str_repeat(' ', strlen($tag['qname']) + 2); } else { $indent .= $this->options[XML_SERIALIZER_OPTION_INDENT_ATTRIBUTES]; } } else { $multiline = false; $indent = false; } if (is_array($tag['content'])) { if (empty($tag['content'])) { $tag['content'] = ''; } } elseif (is_scalar($tag['content']) && (string) $tag['content'] == '') { $tag['content'] = ''; } // replace XML entities (only needed, if this is not a nested call) if ($firstCall === true) { if ($this->options[XML_SERIALIZER_OPTION_CDATA_SECTIONS] === true) { $replaceEntities = XML_UTIL_CDATA_SECTION; } else { $replaceEntities = $this->options[XML_SERIALIZER_OPTION_ENTITIES]; } } else { $replaceEntities = XML_SERIALIZER_ENTITIES_NONE; } if (is_scalar($tag['content']) || is_null($tag['content'])) { if ($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC]) { if ($firstCall === true) { $tag['content'] = call_user_func($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC], $tag['content']); } $tag['attributes'] = array_map($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC], $tag['attributes']); } $tag = XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $this->options[XML_SERIALIZER_OPTION_LINEBREAKS]); } elseif (is_array($tag['content'])) { $tag = $this->_serializeArray($tag['content'], $tag['qname'], $tag['attributes']); } elseif (is_object($tag['content'])) { $tag = $this->_serializeObject($tag['content'], $tag['qname'], $tag['attributes']); } elseif (is_resource($tag['content'])) { settype($tag['content'], 'string'); if ($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC]) { if ($replaceEntities === true) { $tag['content'] = call_user_func($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC], $tag['content']); } $tag['attributes'] = array_map($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC], $tag['attributes']); } $tag = XML_Util::createTagFromArray($tag, $replaceEntities); } return $tag; }
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 & Hutch', 'channel' => 'ABC'); print "creating a attribute string, entities in values already had been replaced:<br>"; print htmlentities(XML_Util::attributesToString($atts, true, false, false, false, XML_UTIL_ENTITIES_NONE)); print "\n<br><br>\n"; /** * using the array-syntax for attributesToString() */ $atts = array('series' => 'Starsky & Hutch', 'channel' => 'ABC'); print "using the array-syntax for attributesToString()<br>"; print htmlentities(XML_Util::attributesToString($atts, array('entities' => XML_UTIL_ENTITIES_NONE)));
/** * create a tag from an array * this method awaits an array in the following format * array( * 'qname' => $tagName, * 'attributes' => array(), * 'content' => $content, // optional * 'namespace' => $namespace // optional * 'namespaceUri' => $namespaceUri // optional * ) * * @access private * @param array $tag tag definition * @param boolean $replaceEntities whether to replace XML entities in content or not * @return string $string XML tag */ function _createXMLTag($tag, $replaceEntities = true) { if ($this->options['namespace'] !== null) { if (is_array($this->options['namespace'])) { $tag['qname'] = $this->options['namespace'][0] . ':' . $tag['qname']; } else { $tag['qname'] = $this->options['namespace'] . ':' . $tag['qname']; } } if ($this->options['indentAttributes'] !== false) { $multiline = true; $indent = str_repeat($this->options['indent'], $this->_tagDepth); if ($this->options['indentAttributes'] == '_auto') { $indent .= str_repeat(' ', strlen($tag['qname']) + 2); } else { $indent .= $this->options['indentAttributes']; } } else { $multiline = false; $indent = false; } if ($replaceEntities) { $replaceEntities = $this->options['replaceEntities']; } if (is_array($tag['content'])) { if (empty($tag['content'])) { $tag['content'] = ''; } } elseif (is_scalar($tag['content']) && (string) $tag['content'] == '') { $tag['content'] = ''; } if (is_scalar($tag['content']) || is_null($tag['content'])) { if ($this->options['encodeFunction']) { if ($replaceEntities === true) { $tag['content'] = call_user_func($this->options['encodeFunction'], $tag['content']); } $tag['attributes'] = array_map($this->options['encodeFunction'], $tag['attributes']); } $tag = XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $this->options['linebreak']); } elseif (is_array($tag['content'])) { $tag = $this->_serializeArray($tag['content'], $tag['qname'], $tag['attributes']); } elseif (is_object($tag['content'])) { $tag = $this->_serializeObject($tag['content'], $tag['qname'], $tag['attributes']); } elseif (is_resource($tag['content'])) { settype($tag['content'], 'string'); if ($this->options['encodeFunction']) { if ($replaceEntities === true) { $tag['content'] = call_user_func($this->options['encodeFunction'], $tag['content']); } $tag['attributes'] = array_map($this->options['encodeFunction'], $tag['attributes']); } $tag = XML_Util::createTagFromArray($tag, $replaceEntities); } return $tag; }
/** * create a tag from an array * this method awaits an array in the following format * array( * "qname" => $tagName, * "attributes" => array(), * "content" => $content, // optional * "namespace" => $namespace // optional * "namespaceUri" => $namespaceUri // optional * ) * * @access private * @param array $tag tag definition * @param boolean $replaceEntities whether to replace XML entities in content or not * @return string $string XML tag */ function _createXMLTag($tag, $replaceEntities = true) { if ($this->options["indentAttributes"] !== false) { $multiline = true; $indent = str_repeat($this->options["indent"], $this->_tagDepth); if ($this->options["indentAttributes"] == "_auto") { $indent .= str_repeat(" ", strlen($tag["qname"]) + 2); } else { $indent .= $this->options["indentAttributes"]; } } else { $multiline = false; $indent = false; } if (is_array($tag["content"])) { if (empty($tag["content"])) { $tag["content"] = ''; } } elseif (is_scalar($tag["content"]) && (string) $tag["content"] == '') { $tag["content"] = ''; } if (is_scalar($tag["content"]) || is_null($tag["content"])) { $tag = XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $this->options["linebreak"]); } elseif (is_array($tag["content"])) { $tag = $this->_serializeArray($tag["content"], $tag["qname"], $tag["attributes"]); } elseif (is_object($tag["content"])) { $tag = $this->_serializeObject($tag["content"], $tag["qname"], $tag["attributes"]); } elseif (is_resource($tag["content"])) { settype($tag["content"], "string"); $tag = XML_Util::createTagFromArray($tag, $replaceEntities); } return $tag; }
function exportXML() { global $i18n, $ClassDir; require_once 'XML/Util.php'; require_once $ClassDir . 'StringHelper.class.php'; $header = array("name", "gender", "birthday", "mobile", "phone", "office_phone", "fax", "addrees", "category", "email", "homepage"); $filename = date("Y_m_d") . "_contact_export.xml"; $xml_data = ""; $xml = new XML_Util(); $xml_data .= $xml->getXMLDeclaration("1.0", "UTF-8") . "\n"; $xml_data .= "" . $xml->createStartElement("contact") . "\n"; // Write contact record $apf_contact = DB_DataObject::factory('ApfContact'); $apf_contact->orderBy('id desc'); $apf_contact->find(); while ($apf_contact->fetch()) { $xml_data .= "\t" . $xml->createStartElement("record") . "\n"; foreach ($header as $title) { $coloum_function = "get" . StringHelper::CamelCaseFromUnderscore($title); $tag = array("qname" => $title, "content" => $apf_contact->{$coloum_function}()); $xml_data .= "\t\t" . $xml->createTagFromArray($tag) . "\n"; } $xml_data .= "\t" . $xml->createEndElement("record") . "\n"; } $xml_data .= "" . $xml->createEndElement("contact") . "\n"; $xml->send($xml_data, $filename); exit; }