...

View Full Version : Problem with an xml format.



jeddi
03-05-2010, 12:22 PM
Hello,

I am trying to process an xml file with simplexml_load_file()
but I notice that the tags that have hyphens in are not working.

I suspect that it's structure is not valid.

This is some of it's structure:


<product>
<id>346</id>
<name>Covered Call Calculator</name>
<description>Earn blah blah blah to maximize profits.</description>
<keywords>option options covered calls stocks </keywords>
<rank>31</rank>
<merchant-name>Mathew Merten</merchant-name>
<merchant-rank>100</merchant-rank>
<date-added>2007-11-28</date-added>
</product>

I use this code to access the tags:


foreach ($xml->xpath('/marketplace/product') as $prod) {

$sql_ins = "INSERT INTO my_temp ( seller, seller_rank, title, descrip, keywords )
VALUES
( '$prod->merchant-name', '$prod->merchant-rank', '$prod_name', $descrip', '$keywords')";

Now the last 3 tags don't work - I assume that it is because they have hyphens in the name ?


Do I need to change those hyphens to underscores to make them work ?

And what would be the best way to make the change ?

( Notice that the date also has legitimate hyphens)

Thanks.



.

abduraooft
03-05-2010, 12:29 PM
The article at http://www.kombitz.com/2010/02/10/php-xml-problem-hyphen-in-the-element-name/ may help.

JAY6390
03-05-2010, 12:54 PM
I would suggest changing them to underscores. It would work that way. You don't have to worry about the hyphens in the node values. They are fine

jeddi
03-05-2010, 01:26 PM
The xml file contains approx 2,000 products and is over 2Mb in size.

Do you suggest that I change use preg_replace to change the entire file
before processing with simplexml_load_file() ?

Something like:



$source = file_get_contents( $file_in );

// Locate all tags with hypens

$pattern = '#(\<.+)-( .+\>)#;
$replacement = '$1_$2';

preg_replace($pattern, $replacement, $source );

file_put_contents( $file_out, $source );


Not sure if my pattern is correct though.



OR
(Just read that post ;) )

Change my code to $link->{'link-code-html'};

that seems to be the better option.

:)

BTW - would my reg ex have worked ?



.

JAY6390
03-05-2010, 01:30 PM
You could also just use a simple str_replace

$find = array('merchant-name', 'merchant-rank', 'date-added');
$replace = array('merchant_name', 'merchant_rank', 'date_added');
$content = str_replace($find, $replace, $content);

jeddi
03-05-2010, 01:43 PM
Well,

I tried changing my script to:


$sql_ins = "INSERT INTO my_temp ( seller, seller_rank, title, descrip, keywords )
VALUES
( '$prod->{'merchant-name'}', '$prod->{'merchant-rank'}', '$prod_name', $descrip', '$keywords')";

But the MYSQL failed - I guess it is because of the double use of single quotes ?

Should I use double quotes (excaped)?

eg:


$sql_ins = "INSERT INTO my_temp ( seller, seller_rank, title, descrip, keywords )
VALUES
( \"$prod->{'merchant-name'}\", \"$prod->{'merchant-rank'}\", '$prod_name', $descrip', '$keywords')";

Or would that be a problem ?



.

JAY6390
03-05-2010, 01:49 PM
Use sprintf instead

$sql_ins = sprintf("INSERT INTO my_temp ( seller, seller_rank, title, descrip, keywords )
VALUES
( '%s', '%s', '%s', '%s', '%s')",$prod->{'merchant-name'}, $prod->{'merchant-rank'}, $prod_name, $descrip, $keywords) ;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum