I've got a situation where I'm using an XML parser to extract data from a php://input.
The data being sent in looks like:-
<this>foo</this>
<that>bar></that>
<data><dong>zebras</dong><fong>monkeys</fong></data>
Now, the issue is that because the handler:-
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$document = file_get_contents("php://input");
xml_parse($xml_parser, $document);
xml_parser_free($xml_parser);
...is interpreting tags for me to decifer the contents to make it useful... It seems to be missing anything within i.e. I need to find out how i can get it to ignore the child/nested elements/tags and just bosh out the data whatever.
Any thoughts muchos help. I've not found much about this out there.
-
I'm not sure if I understood the question correctly, but
prints<?php function contents($parser, $data) { echo $data; }
$xml_parser = xml_parser_create(); xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); // xml_set_element_handler($xml_parser, "startTag", "endTag"); xml_set_character_data_handler($xml_parser, "contents"); // $document = file_get_contents("php://input"); $document = '<x><this>foo</this><that>bar</that><data><dong>zebras</dong><fong>monkeys</fong></data></x>'; xml_parse($xml_parser, $document); xml_parser_free($xml_parser);
foobarzebrasmonkeys
WiseDonkey : Hi Volkerk, Thanks for the response. What i need it to do it say foobarzebras monkeys i.e. withinn all contents is printed, almost as source, with tags. -
WideDonkey, have you considered using the DOM instead? You can easily do :
$dom = new DOMDocument(); $dom->loadXML(file_get_contents('php://input')); $data = $dom->getElementsByTagName('data'); $data = $data[0]->asXML();
-
Comment on Evert's answer: DOMNodeElement doesn't have a method asXML(), $data = $data[0]->asXML(); will not work with PHP-DOM. And SimpleXMLElement::asXML() will return the whole element's xml, i.e. <data>...</data>
But you can modify the XML Element Structure Example to print the tags in your element handler functions when the counter is > 1 -
What I ended up doing was using SimpleXML to do:-
$xml = new SimpleXMLElement($input); $whatIwant = $xml->bar->monkeys
You can go down as many levels as you want such as test
The $xml is the furthest out wrapper tag.
-
phpQuery gives you all the power of jQuery Selectors wrapped up in a PHP shell.
phpQuery::newDocumentFileXHTML('my-xhtml.html')->find('p');
0 comments:
Post a Comment