Example of using functions to read XML files in php

Source: Internet
Author: User
Example of using functions to read XML files in php
  • Else
  • Echo"";
  • }

  • Function content ($ x, $ content ){

  • Echo $ content;
  • }

  • $ Printerror = false;

  • $ Xmlfile = "user. xml ";

  • // Read data in step 2
  • $ Fp = fopen ($ xmlfile, "r ");

  • While (! Feof ($ fp )){

  • $ Data = fread ($ fp, 1024 );
  • // Start parsing
  • If (! Xml_parse ($ xml, $ data )){
  • $ Printerror = true;
  • }
  • }

  • // Close the file

  • Fclose ($ fp );
  • If ($ printerror ){
  • $ Row = xml_get_current_line_number ($ xml );
  • $ Col = xml_get_current_column_number ($ xml );
  • $ Errormess = xml_error_string (xml_get_error_code ($ xml ));
  • Echo "in the file {$ xmlfile}, [{$ row} row, {$ col} column]: {$ errormess }.";
  • }
  • // Close the parser
  • Xml_parser_free ($ xml );
  • ?>

  • ";

    1. // Step 1 create the parser xml_parser_create (encoding );

    2. $ Xml = xml_parser_create ('utf-8 ');
    3. // Xml_parser_set_option-set options for specified XML parsing
    4. // Xml_parser_set_option ($ xml, XML_OPTION_CASE_FOLDING, false); false is output as is
    5. // Register the event. what functions will be used to start and end table timing?
    6. Xml_set_element_handler ($ xml, "starttag", "endtag ");

    7. Xml_set_character_data_handler ($ xml, "content ");

    8. Function starttag ($ x, $ tagName, $ args ){

    9. If ($ tagName = "USERS ")
    10. Echo"
    11. Echo"
    12. } Else
    13. Echo"
    14. "; ";
      {$ TagName }";
    15. Else if ($ tagName = "USER "){
    16. Echo"
    17. {$ Args ['id']}";
    18. }

    19. Function endtag ($ x, $ tagName ){

    20. If ($ tagName = "USERS ")
    21. Echo"
    22. ";
    23. Else if ($ tagName = "USER ")
    24. Echo"

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.