XML parsing conundrum

后端 未结 7 1400
甜味超标
甜味超标 2021-01-15 22:25

UPDATE: I\'ve reworked the question, to show progress I\'ve made, and maybe make it easier to answer.

UPDATE 2: I\'ve added another value to the XML. Extension avail

相关标签:
7条回答
  • 2021-01-15 23:02

    I prefer DOM DOcument and XPath myself so his is what I'd do...

    $xml = '\path\to\your\file.xml';
    $doc = new DOMDocument( '1.0', 'UTF-8' );
    $doc->load( $xml );
    
    $dxpath = new DOMXPath( $doc );
    $items = $dxpath->query( '//Item' );
    
    $db = new PDO( 'mysql:dbname=YOURDB:host=YOURHOST', $DBUSER, $DBPASS );
    $ins = $db->prepare('
                        INSERT INTO ur_table
                        ( `platform` , `name` , `title` , `path` )
                        VALUES
                        ( :platform , :name , :title , :path );
                        ');
    
    foreach( $items as $item )
    {
        $ins->bindValue( ':platform'     , $item->getElementsByTagName( 'PlatForm' )->item(0)->nodeValue , PDO::PARAM_STR );
        $ins->bindValue( ':name'         , $item->getElementsByTagName( 'Name' )->item(0)->nodeValue     , PDO::PARAM_STR );
        $ins->bindValue( ':title'        , $item->getElementsByTagName( 'Title' )->item(0)->nodeValue    , PDO::PARAM_STR );
        $ins->bindValue( ':DownloadPath' , $item->getElementsByTagName( 'PlatForm' )->item(0)->nodeValue , PDO::PARAM_STR );
        $ins->execute();
    }
    

    No need for stripslashes and what not - it will handle all taht for you.

    0 讨论(0)
提交回复
热议问题