Home >Backend Development >PHP Tutorial >Example of using php to read XML files using functions

Example of using php to read XML files using functions

WBOY
WBOYOriginal
2016-07-25 09:10:30924browse
";
  • else
  • echo "";
  • }

  • function content($x,$content){

  • echo $content;
  • }

  • $printerror=false;

  • $xmlfile="user.xml";

  • //The second step is to read the data
  • $fp=fopen($xmlfile,"r");

  • while(!feof($fp)){

  • $data=fread($fp,1024);
  • //Start parsing
  • if (! ){
  • $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);
  • ?>

  • Copy code
    1. //The first step is to create a parser xml_parser_create(encoding);

    2. $xml = xml_parser_create('utf-8');
    3. //xml_parser_set_option — parse the specified XML Set options
    4. //xml_parser_set_option($xml,XML_OPTION_CASE_FOLDING,false); false output as it is
    5. //Register event, what function 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. ";
    13. }else
    14. echo "
    15. {$tagName}";
    16. else if($tagName == "USER") {
    17. echo "
    18. {$args['ID']} ";
    19. }
    20. function endtag($x,$tagName){

    21. if($tagName=="USERS")
    22. echo "
    23. ";
    24. else if($tagName = = "USER")
    25. echo "
    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn