Avatar billede suicide Nybegynder
04. juli 2003 - 10:34 Der er 2 kommentarer

xml i php

Jeg har dette i min xml fil:

<bf:event name="createPlayer" timestamp="18.8803">
  <bf:param type="int" name="player_id">3</bf:param>
  <bf:param type="vec3" name="player_location">457.128/41.94/220.76</bf:param>
  <bf:param type="string" name="name">Playername</bf:param>
  <bf:param type="int" name="is_ai">0</bf:param>
  <bf:param type="int" name="team">1</bf:param>
  </bf:event>

Er der nogen der kan fortælle mig hvor nedenstående ikke virker ?? Det er echo sætningen der ikke outputter noget.

<?php

$open_tags = array(
    'START' => '<bf:event name="createPlayer"',
    'PLAYERID' => '<bf:param type="int" name="player_id">',
    'PLAYERNAME' => '<bf:param type="string" name="name">');

$close_tags = array(
    'START' => '</bf:event>',
    'PLAYERID' => '</bf:param>',
    'PLAYERNAME' => '</bf:param>');
   
?>

<?php

// handles the attributes for opening tags
// $attrs is a multidimensional array keyed by attribute
// name and having the value of that attribute
function startElement($parser, $name, $attrs=''){
    global $open_tags, $temp, $current_tag;
    $current_tag = $name;
    if ($format = $open_tags[$name]){
    switch($name){
        case 'START':
        echo 'New event: ';
        break;
        default:
        break;
    }
    }
}

// $current_tag lets us know what tag we are currently
// dealing with - we use that later in the characterData
// function.
//
// when we see a </STORY> we know that it is time to
// flush our temp variables and prepare to move onto
// the next one
function endElement($parser, $name, $attrs=''){
    global $close_tags, $temp, $current_tag;
    if ($format = $close_tags[$name]){
    switch($name){
        case 'START':
        return_page($temp);
        $temp = '';
        break;
        default:
        break;
    }
    }
}

// this function is passed data between elements
// theu $data would equal 'Title Here'
// in the line <TITLE>Title Here</TITLE>
function characterData($parser, $data){
    global $current_tag, $temp, $catID;
    switch($current_tag){
    case 'PLAYERID':
        $temp['playerid'] = $data;
        $current_tag = '';
        break;
    case 'PLAYERNAME':
        $temp['playername'] = $data;
        $current_tag = '';
        break;
    default:
        break;
    }
}
?>

As you can see so far XML parsing in PHP isn't all that bad. Now for the fun part - parsing the file! For that you will need the rest of the code, which is fairly simple.

<?php

function return_page(){
    global $temp;
    echo ''.$temp['playerid'].' . <br> . '.$temp['playername'].' . <br><br>';
}

// what are we parsing?
$xml_file = 'blabla.xml';

// declare the character set - UTF-8 is the default
$type = 'UTF-8';

// create our parser
$xml_parser = xml_parser_create($type);

// set some parser options
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');

// this tells PHP what functions to call when it finds an element
// these funcitons also handle the element's attributes
xml_set_element_handler($xml_parser, 'startElement','endElement');

// this tells PHP what function to use on the character data
xml_set_character_data_handler($xml_parser, 'characterData');

if (!($fp = fopen($xml_file, 'r'))) {
    die("Could not open $xml_file for parsing!\n");
}

// loop through the file and parse baby!
while ($data = fread($fp, 4096)) {
    if (!($data = utf8_encode($data))) {
        echo 'ERROR'."\n";
    }
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf( "XML error: %s at line %d\n\n",
        xml_error_string(xml_get_error_code($xml_parser)),
        xml_get_current_line_number($xml_parser)));
    }
}

xml_parser_free($xml_parser);

?>
Avatar billede mxs Nybegynder
22. maj 2004 - 12:48 #1
Øøøøøøh hvad er det lige du vil have os til at se?
Avatar billede krydset Nybegynder
16. juni 2005 - 21:33 #2
Du mangler nok en ;
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester