5 Replies - 1324 Views - Last Post: 26 February 2009 - 01:34 PM Rate Topic: -----

#1 hrvoje89   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 05-February 09

DOM and reading XML problem...

Post icon  Posted 24 February 2009 - 12:32 PM

Hello all...again me with my problems and no solutions :)

So now i have a XML problem...

I have XML:
- <Info>
- <map>
- <element>
  <key>LINKS_TYPE</key> 
- <value>
- <list>
  <element SortOrder="0" Text="Specification" Source="http://eu.computers.toshiba-europe.com/cgi-bin/ToshibaCSG/jsp/productPage.do?service=EU&PRODUCT_ID=150493" InfoTypeId="2" /> 
  </list>
  </value>
  </element>
- <element>
  <key>VENDORS_LINKS_TYPE</key> 
- <value>
  <list /> 
  </value>
  </element>
- <element>
  <key>BENIFITS_FEATURES_TYPE</key> 
- <value>
  <list /> 
  </value>
  </element>
- <element>
  <key>MARKETING_TYPE</key> 
- <value>
- <list>
  <element SortOrder="0" Text="The Satellite A300 has the widest range of models available so you get to choose the machine configurations that are perfect for your needs and your pocket. The laptop features a new, striking and highly durable design using the Toshiba special glossy Fusion finish. A fully integrated Touch Pad ensures maximum user comfort." Source="" /> 
  </list>
  </value>
  </element>
  </map>
  </Info>



And I need to extract this text from tag:
<element SortOrder="0" Text="The Satellite A300 has the widest range of models available so you get to choose the machine configurations that are perfect for your needs and your pocket. The laptop features a new, striking and highly durable design using the Toshiba special glossy Fusion finish. A fully integrated Touch Pad ensures maximum user comfort." Source="" />


I am using code:
			 $doc = new DOMDocument();
			 $doc->load( 'artikli/test.xml' );
			 
			 $books = $doc->getElementsByTagName( "Info" );
			 
			 foreach( $books as $book )
  { ... etc.... 


But i can't get pass
  <key>LINKS_TYPE</key> 
... so please help me :)

Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: DOM and reading XML problem...

#2 Valek   User is offline

  • Gravity seems weak until you look down
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,716
  • Joined: 08-November 08

Re: DOM and reading XML problem...

Posted 24 February 2009 - 01:37 PM

You might consider using SimpleXML instead, as it's explicitly for parsing XML documents, and I've found it's more user-friendly with them than DOMDocument is.

This post has been edited by Valek: 24 February 2009 - 01:37 PM

Was This Post Helpful? 0
  • +
  • -

#3 hrvoje89   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 05-February 09

Re: DOM and reading XML problem...

Posted 24 February 2009 - 01:55 PM

yes i know..but again how do i get data i need??? (using simpleXML) ??

the problems is that I don't have different tags...just tag content is different :(

please help

ty
Was This Post Helpful? 0
  • +
  • -

#4 Valek   User is offline

  • Gravity seems weak until you look down
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,716
  • Joined: 08-November 08

Re: DOM and reading XML problem...

Posted 25 February 2009 - 02:08 AM

Well, first thing's first, your XML was not well-formed. You'll notice in the LINKS_TYPE section, the link in that element's source attribute contains an ampersand. To make your XML well-formed, you must change that ampersand to &amp;, or SimpleXML is going to throw errors at you, and it won't work correctly.

Now, once you've done that, give this code here a try. I've commented it so you can see exactly what it's doing.

<?php

//Load your XML file.
$object = simplexml_load_file("file.xml");

//Create your array for elements to be stored in.
$elemarray = Array();

//Cycle through each element tag..
for($i=0; $object->map->element[$i]; $i++) {
	// Look to see if an internal element tag is set.
	if(isset($object->map->element[$i]->value->list->element)) {
		//If it is, grab the attributes of it with SimpleXML's attributes() function, and store that data in our element array.
		foreach($object->map->element[$i]->value->list->element->attributes() as $key => $val) {
			$elemarray[$i][$key] = (string)$val;
		}
	}
}

//Re-index the array.
$elemarray = array_values($elemarray);

//Display the data as stored by PHP.
echo "<pre>";
print_r($elemarray);
echo "</pre>";

?>


Hope this helps :)

This post has been edited by Valek: 25 February 2009 - 02:11 AM

Was This Post Helpful? 0
  • +
  • -

#5 hrvoje89   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 05-February 09

Re: DOM and reading XML problem...

Posted 26 February 2009 - 03:22 AM

Well... The XML is not formed by me!!

It's formed by API of Asbis.... XML is very very bad formed if you ask me... :)
so... there is no simple way that i can change form of XML... so i need to
manage with this XML :)
Was This Post Helpful? 0
  • +
  • -

#6 Valek   User is offline

  • Gravity seems weak until you look down
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,716
  • Joined: 08-November 08

Re: DOM and reading XML problem...

Posted 26 February 2009 - 01:34 PM

Actually, you could do this:

$variable = file_get_contents("link to API's XML output here");
$variable = str_replace("&", "&amp;",$variable);
file_put_contents("filename goes here",$variable);
$xml = simplexml_load_file("filename");
//XML code here.


This way, you can cache the data too, if you need to. You could write a caching function around that that only updates it every so often, and will otherwise load the saved data from your server, thereby speeding up the process. This also eliminates the XML forming problem, so your code should work seamlessly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1