开发者

PHP XML Parsing Question

开发者 https://www.devze.com 2023-02-09 16:14 出处:网络
I sucessfully wrote a site that parses a PHP file and deals with the opening / closing tags by replaging it with HTML.

I sucessfully wrote a site that parses a PHP file and deals with the opening / closing tags by replaging it with HTML.

The thing is I only know how to parse the entire file. What if I wanted to only parse some of the file?

I already know how to loop through the XML tags to find the section that I want, but I don't know how to through that into the parsing code I already have.

This is the parsing code I am using now:

$p = xml_parser_create();

//Set the handling functions

xml_set_element_handler ($p, 'handle_open_element', 'handle_close_element');
xml_set_character_data_handler ($p, 'handle_character_data');

// read the file:

    $file = 'books3.xml';
    $fp = @fopen ($file, 'r') or die ("Could not open a file called '$file' .\n</body>\n</html>");
    while ($data = fread ($fp, filesize($file))) {
    xml_parse ($p, $data, feof($fp));
    }

    //free up parser
    xml_parser_free($p);

Books3.xml is the file. There are functions above that deal with opening and closing tags. How could I edit this so that I only sen开发者_C百科d a portion of books3.xml to the functions.

Any ideas?

Thanks for your help


I believe the XML parser functions are very low level and a bit complicated. If you need to parse only small XML files it might be a lot easier to use SimpleXML functions. They allow you to access the XML like nested PHP datastructures.

If you need a stream oriented parsing extension (large files) XMLReader might be the easier way.

0

精彩评论

暂无评论...
验证码 换一张
取 消