Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Expat getting attribute names?

Status
Not open for further replies.

alohaaaron

Programmer
Mar 27, 2008
80
US
Hi, I'm able to parse out XML elements and get the values of the attributes but not the names. For example I get ContactPricing and Price and the values 20 and 28 but I can't get the "email" or "phone".

Code:
$parser=xml_parser_create();
$element_attrs = array();

function start($parser,$element_name,$element_attrs) {
  global $element_attrs; //array of attributes
}

function stop($parser,$element_name) {
  echo "<br />";
}

function characterData($parser,$data) {
  global $element_attrs; //array of attributes
  echo $data;

  if (is_array($element_attrs)) {
      while(list($key,$val) = each($element_attrs)) {
        echo "<br>$key</br>"; //doesn't print out anything
        echo "<br>$val</br>"; //doesn't print out anything		
      }
  }
}


test data in xml file
<ContactPricing>
     <Price contactType="email">20</Price>
     <Price contactType="phone">28</Price>
</ContactPricing>
 
for those of you that might like to know how this was resolved, or how to use xml_parser without using the more usual xml_parse_into_struct, you need to set the default handler (or discrete character and element handlers) with the relevant xml_set_*_handler() functions and then create the handlers to deal with the data. Although the OP had half-hearted handlers in the original post, he had failed to tell php what the handlers actually were, and also had failed actually to parse the xml with xml_parse.

as (nearly) always, the php manual gives all the necessary information

however, this is not a sensible method of parsing xml data to extract variables. xml_parse_into_struct() is a more logical choice and if you are using a system that is less than a few years old (or if older then upgrade-blocked) then the simplexml extension is far more flexible.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top