Jump to content

parse xml with php questions


ballhogjoni

Recommended Posts

I want to extract certain parts of the following xml doc but I am using the script from php.net to do so. I only need to echo a few select tags. The php code parses and echoes all of the xml file, but I only want to echo the <creditcard></creditcard> and <issuer></issuer> information. How would I go abut making those changes to the php code?

 

Sample of my XML file:

<card>
- <cid>
- <![CDATA[ <ELEMENT>1033<ELEMENT>
  ]]> 
  </cid>
- <creditcard>
- <![CDATA[ <ELEMENT>Advanta Platinum BusinessCard<ELEMENT>
  ]]> 
  </creditcard>
- <issuer>
- <![CDATA[ <ELEMENT>Advanta<ELEMENT>
  ]]> 
  </issuer>
- <category>
- <![CDATA[ <ELEMENT>Business<ELEMENT>
  ]]> 
  </category>
- <status>
- <![CDATA[ <ELEMENT>Platinum<ELEMENT>
  ]]> 
  </status>
- <cardtype>
- <![CDATA[ <ELEMENT>MasterCard<ELEMENT>
  ]]> 
  </cardtype>
- <introductoryrate>
- <![CDATA[ <ELEMENT><ELEMENT>
  ]]> 
  </introductoryrate>
- <timeperiod>
- <![CDATA[ <ELEMENT><ELEMENT>
  ]]> 
  </timeperiod>
- <appliedto>
- <![CDATA[ <ELEMENT>Purchases, Balance Transfers<ELEMENT>
  ]]> 
  </appliedto>
</card>

 

this is my php code

 

<?php
$file = "http://www.xxxxxxxxxxxxxxxx.com/partners/access/xml/xml.asp?uid=xxxxxxxxxxxxxxxxx";
$map_array = array(
    "BOLD"     => "B",
    "EMPHASIS" => "I",
    "LITERAL"  => "TT"
);

function startElement($parser, $name, $attrs) 
{
    global $map_array;
    if (isset($map_array[$name])) {
        echo "<$map_array[$name]>";
    }
}

function endElement($parser, $name) 
{
    global $map_array;
    if (isset($map_array[$name])) {
        echo "</$map_array[$name]>";
    }
}

function characterData($parser, $data) 
{
    echo $data;
}

$xml_parser = xml_parser_create();
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf("XML error: %s at line %d",
                    xml_error_string(xml_get_error_code($xml_parser)),
                    xml_get_current_line_number($xml_parser)));
    }
}
xml_parser_free($xml_parser);
?>

Link to comment
https://forums.phpfreaks.com/topic/71679-parse-xml-with-php-questions/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.