function test_basics() { $testXml = new _testXml(); $testXml->rootElement = 'mAiN'; $testXml->preserveCase = true; $fixPathTests = array('/mAiN/TEST/1/STUFF' => '/mAiN/0/TEST/1/STUFF/0', '/' => '', '/mAiN/0/TEST' => '/mAiN/0/TEST/0', '/mAiN/0/TEST/0/ONE/1' => '/mAiN/0/TEST/0/ONE/1'); try { foreach ($fixPathTests as $fixThis => $matchesThis) { $testXml->preserveCase = true; $this->assertEquals($testXml->fix_path($fixThis), $matchesThis); $testXml->preserveCase = false; $this->assertEquals($testXml->fix_path($fixThis), strtoupper($matchesThis)); } } catch (Exception $e) { throw new exception("Failed basic fix_path() test... " . $e->getMessage()); } $xml = new cs_phpxmlCreator('cart', null, true); $xml->add_attribute("/cart", array("foo" => "bar")); $xml->add_tag("/cart/item/name", "foo"); $xml->add_attribute("/cart/item", array('comment' => "1")); //this REPLACES all attributes with the given array. $xml->add_tag("/cart/item/value", "lots"); $xml->add_tag("/cart/item/extra", null); $xml->add_tag("/cart/item/extra", null, array('location' => "the internet")); $xml->add_tag("/cart/item/1/name", "bar"); $xml->add_tag("/cart/item/1/value", "even more"); $xml->add_attribute("/cart/item/1", array('comment' => "2")); $xml->add_attribute("/cart/item/1/value", array('currency' => "USD")); $xml->add_tag("/cart/item/1/extra/0", null, array('location' => "unknown")); //faster than adding attribs later. $xml->add_tag("/cart/item/1/extra/1/magic", "STUFFING!", array("first" => "the first tag", "second" => "second tag")); $xml->add_tag("/cart/item/1/extra/1/extra", null); $xml->add_tag("/cart/extra/magic", "STUFFING!", array("first" => "the first tag", "second" => "second tag")); $xml->add_tag("/cart/extra/extra", null); $xml->add_tag("/cart/tag.with.dots.in.it/item", "value of tag with dots"); $testFileContents = file_get_contents(dirname(__FILE__) . '/files/basic.xml'); $testFileContents = preg_replace("/\n\$/", '', $testFileContents); $generatedXML = $xml->create_xml_string(); if (!$this->assertEquals(serialize($testFileContents), serialize($generatedXML))) { $this->gfObj->debug_print(htmlentities(serialize($testFileContents))); $this->gfObj->debug_print(htmlentities(serialize($generatedXML))); } $parser = new cs_phpxmlParser($generatedXML); #$this->gfObj->debug_print($xml->load_xmlparser_data()); $parser->get_tree(); }
/** * Takes an XMLParser object & loads data from it as the internal XML array. This * facilitates the ability to add data to existing XML. */ public function load_xmlparser_data(cs_phpxmlParser $obj) { //TODO: need to be able to re-populate $this->tags & $this->attributes $data = $obj->get_tree(); $this->xmlArray = $data; $this->a2p = new cs_arrayToPath($data); $x = array_keys($this->a2p->get_data(NULL)); if (count($x) > 1) { throw new exception(__METHOD__ . ": too many root elements"); } else { $this->rootElement = $x[0]; } }