Extracting certain information from an XML file

I’ve been all over the internet trying to find out how to do this but I don’t think I have enough knowledge of PHP and XML to put them together.

What I want to do I believe is fairly simple. I want to take a certain piece of information out of an XML file and then use it within my PHP file. I want to do this for two separate mini-projects but I’ll ask about the simpler one. Here’s the XML file (http://wowarmory.com/character-talents. … s&n=Nicora):

[code]<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet type="text/xsl" href="/layout/character-talents.xsl"?> [/code]

What I want is the really long number labelled the talentTree value. So, how can I get it out to then be assigned to a variable and used in my code?

Thanks in advance for the help.

use regex or the PHP-XML functiond:

regex should be easear in that case:

/<talentTree value="([^"]*)"/

http://php.net/regex

http://php.net/xml

use xml parser class…
i personally suggest SofeeXmlParser
you can find it at http://phpclasses.org :wink:

Okay, I got it work…sort of. I decided to use SimpleXML because it’s simple and I can get the value I wanted except for one thing. The XML file has to be local in order for me to use the script on it. Anything that I try to parse that’s not on my domain won’t work. I haven’t asked my hosting company but I’m pretty sure that it’s some sort of security preference they have set.

Before I go bugging them about it, is there some sort of work-around I can implement? Here’s the phpinfo for my domain in case someone wants to see.

should work:

Registered PHP Streams php, http, ftp, https, ftps, compress.bzip2, compress.zlib

what function are u using to access the data?

Well, here’s the php code where I reference the data:

[code]<?php
require_once(“XML.inc.php”);

$xml_file=“http://wowarmory.com/character-talents.xml?r=Hydraxis&n=Nicora”;
#$xml_data=implode("",file($xml_file)); //get XML data for alternative direct parsing

$xml=new XML();
$xml->file_read($xml_file);
#$xml->parse($xml_data); //parse direct XML data
$xml->parse();
#$xml->debug();

//Samples…
echo $xml->page->_param[“lang”];
echo $xml->page->characterInfo->talentTab->talentTree->_param[“value”];

?>[/code]

Should I using another class to access the data?

what class r u using now? i thougs u decided to use simplexml.

could u try this code:
[php]<?php
error_reporting(E_ALL);
echo(file_get_contents(‘http://wowarmory.com/character-talents.xml?r=Hydraxis&n=Nicora’));
[/php]

I was trying to do the same thing. I found it easier to convert it to mysql and the pull the information from your mysql server if you have one.

Q1712:

I’m assuming that you didn’t mean to put that extra <?php at the beginning of your code. Anyways, I place the code in a separate file here: http://wow.wolfdragon.net/get_contents.php. And yes, I am using simpleXML, the code for that is contained in the XML.inc.php file. I can post it if needed.

bustersnm:

I have a mysql server. What script did you use to convert it? And does it do it dynamically, because the XML data changes all the time?

I tried using the code that Q1712 wrote in my original file like this:

[code]<?php
error_reporting(E_ALL);
require_once(“XML.inc.php”);

$xml_file=file_get_contents(‘http://wowarmory.com/character-talents.xml?r=Hydraxis&n=Nicora’);
#$xml_data=implode("",file($xml_file)); //get XML data for alternative direct parsing

$xml=new XML();
$xml->file_read($xml_file);
#$xml->parse($xml_data); //parse direct XML data
$xml->parse();
#$xml->debug();

//Samples…
echo $xml;
//echo $xml->page->_param[“lang”];
//echo $xml->page->characterInfo->talentTab->talentTree->_param[“value”];

?>[/code]

So what I’m guessing I should be seeing (actually what I want to see) is the XML just echoed for me. What I get is

Object

Nothing else, not even in the source code.

cause $xml is an object (an instance of a class), not a string.

but it seems that the data is recived properly.

i thing would be good if we could have a look at the XML.inc.php

Here it is:

[code]<?php
/*

  • XML.inc.php
  • Class to convert an XML file into an object
  • Copyright © 2006 Oliver Strecke [email protected]
  • This library is free software; you can redistribute it and/or
  • modify it under the terms of the GNU Lesser General Public
  • License as published by the Free Software Foundation; either
  • version 2 of the License, or (at your option) any later version.
  • This library is distributed in the hope that it will be useful,
  • but WITHOUT ANY WARRANTY; without even the implied warranty of
  • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  • Lesser General Public License for more details.
  • You should have received a copy of the GNU Lesser General Public
  • License along with this library; if not, write to the Free Software
  • Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    */

class XML{
var $_parser;
var $_xml_data;
var $_actual_tag;

//Constructor...
function xml(){
    $this->_parser=xml_parser_create("ISO-8859-1");
    $this->_xml_data="";
    $this->_actual_tag=$this;

    xml_set_object($this->_parser,$this);
    xml_parser_set_option($this->_parser,XML_OPTION_CASE_FOLDING,false);
    xml_set_element_handler($this->_parser,"tag_open","tag_close");
    xml_set_character_data_handler($this->_parser,"tag_data");
    xml_set_default_handler($this->_parser,"tag_data");
}

//get XML data from file...
function file_read($xml_file){
	if(file_exists($xml_file)){
		$this->_xml_data=implode("",file($xml_file));
	}
	return 1;
}

//parse XML data...
function parse($xml_data=0){
	if($xml_data)$this->_xml_data=$xml_data;
	xml_parse($this->_parser,$this->_xml_data);
    xml_parser_free($this->_parser);
    return 1;
}

function tag_open($parser,$name,$attrs){
	//create new tag...
	$tag=new XML_TAG(&$this->_actual_tag);
	$tag->_name=$name;
	$tag->_param=$attrs;

	//add tag object to parent/actual tag object...
	if(is_a($this->_actual_tag,"XML_TAG")){
		if(is_object($this->_actual_tag->$name) || is_array($this->_actual_tag->$name)){
			//same child objects -> Array...
			$last_index=$this->_actual_tag->new_child_array($tag,$name);
			$this->_actual_tag=&$this->_actual_tag->{$name}[$last_index];
		}else{
			//add new child object to actual tag...
			$this->_actual_tag->new_child($tag,$name);
		    $this->_actual_tag=&$this->_actual_tag->$name;
		}
	}else{
		//copy first tag object in this object...
		$this->$name=$tag;
		$this->_actual_tag=&$this->{$name};
	}
	return 1;
}

function tag_data($parser,$string){
	if(strlen(trim($string))>0)$this->_actual_tag->_value=$string;
    return 1;
}

function tag_close($parser,$name){
    $this->_actual_tag=&$this->_actual_tag->_parent;
    return 1;
}

//Debug...
function debug($exit=0){
	echo "<pre>";
	print_r($this);
	echo "</pre>";
	if($exit)exit;
}

}

class XML_TAG{
var $_parent;
var $_name;
var $_value;
var $_param;

//Constructor...
function xml_tag($parent){
    $this->_parent=&$parent;
	$this->_name="";
	$this->_value=false;
	$this->_param=Array();
	return 1;
}

//simply add ne child to this object...
function new_child($child,$child_name){
	$this->$child_name=&$child;
}

//add child array for more same childs to this object...
function new_child_array($child,$child_name){
	//create array and set old child object to the first array element...
	if(is_object($this->$child_name)){
		$tmp_obj=$this->$child_name;
		$this->$child_name=Array();
		$this->new_child_array($tmp_obj,$child_name);
	}
	//push child reference into child array...
	$this->{$child_name}[]=&$child;
	$last_index=count($this->$child_name)-1;
	return $last_index;
}

//Debug...
function debug(){
  echo "<pre>";
  print_r($this);
  echo "</pre>";
}

}
?>[/code]

//get XML data from file... function file_read($xml_file){ if([b]file_exists($xml_file)[/b]){ $this->_xml_data=implode("",file($xml_file)); } return 1; }

file_exists dosn’t work with URL’s.

http://php.net/file_exists

thats why using this class with url will fail.

u may wanna contact the author about that.

I use the ExcelMySQLConverter from soft galexy, to convert to a sql. then you just upload it into your mysql.

that is why Q i need help with that codeing

Is there something I can use instead of file_exists that will work with URLs?

Thanks, but I was looking for something I wouldn’t have to buy. If it comes down to it, I’ll reconsider it.

delete the if. but than u have to be sure the url is woking or have to catch the xml-parse-error.

So I removed the if statement

//get XML data from file... function file_read($xml_file){ $this->_xml_data=implode("",file($xml_file)); return 1; }

And I get this: http://wow.wolfdragon.net/test.php

It’s not quite working yet.

i guess u now passes the already loaded xmldata to the file_read() function.

file_read() needs a filename/url as argument.

now that u deleted the if, try the code u used in the very beginning.

So I changed the code back to this:

[code]<?php
require_once(“XML.inc.php”);

$xml_file=“http://wowarmory.com/character-talents.xml?r=Hydraxis&n=Nicora”;
#$xml_data=implode("",file($xml_file)); //get XML data for alternative direct parsing

$xml=new XML();
$xml->file_read($xml_file);
#$xml->parse($xml_data); //parse direct XML data
$xml->parse();
#$xml->debug();

//Samples…
echo $xml->page->_param[“lang”];
echo $xml->page->characterInfo->talentTab->talentTree->_param[“value”];

?>[/code]

And all I get is a blank page again: http://wow.wolfdragon.net/test.php

Sponsor our Newsletter | Privacy Policy | Terms of Service