Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.