1

I have XML file

<shop>
<categories>
<category id="-2147483638" parent="0" name="Dia"/>
<category id="-2147483637" parent="0" name="Stri"/>
<categories>
<parameters>
<parameter id="6" name="Druh" typ="ENUM" systemid="">
<enumValue valueId="5" value="Be"/>
<enumValue valueId="6" value="Zi"/>
<enumValue valueId="7" value="Po"/>
<enumValue valueId="8" value="Di"/>
<enumValue valueId="9" value="Pr"/>
<enumValue valueId="10" value="Ma"/>
<enumValue valueId="11" value="Ma"/>
<enumValue valueId="12" value="Gr"/>
<enumValue valueId="13" value="Ak"/>
<enumValue valueId="14" value="Ru"/>
<enumValue valueId="15" value="Za"/>
<enumValue valueId="16" value="Kr"/>
<enumValue valueId="17" value="Pe"/>
</parameter>
<parameter id="9" name="Šperk" typ="ENUM" systemid="s9">
<enumValue valueId="4" value="Male"/>  
<enumValue valueId="5" value="Female"/>
<enumValue valueId="6" value="Unisex"/>
</parameter>
</parameters>
</shop>

and PHP code for reading the xml:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
SET NAMES 'utf8'; 
TRUNCATE TABLE ocppoption;
truncate table ocppoption_description ;

<?

$request_url = "http://...xml"; // 
$xml = simplexml_load_file($request_url) or die("error");    
$language=2;


foreach($xml->parameters->parameter as $key) {
  echo "\n\r<br/><br/>insert into option  values (
".$key['id']."
, 'select'    
,1
);";

  echo "\n\r<br/>insert into option_description  values (
".$key['id']."
, 1
,'".$key['name']."'
);";


if (!isset($j))   {$j=1;}

   $i=0; 
    $k=1;
    $i=$j; 

foreach($key->attributes()  as $enum => $val) {

    echo "\n\n\r<br/><br/>insert into option_value  values (
    ".$i++."
,".$key['id']."
    ,''
    , ".$k++."
    );";    
  $j=$i;
}}

I have output:

    insert into option values ( 6 , 'select' ,1 );
    insert into option_description values ( 6 , 1 ,'Druh' );

    insert into option_value values ( 1 ,6 ,'' , 1 );

    insert into option_value values ( 2 ,6 ,'' , 2 );

    insert into option_value values ( 3 ,6 ,'' , 3 ); 

but if I want to load values from tag

<enumValue valueId="4" value="Male"/>. 

so I want output like insert into option_value values ( 4, 6 ,'' , 3 );

It didnt work correctly. Can you please help me? I need to have output 4, Male... how can I get this values from xml?

1 Answer 1

1
echo $xml->enumValue["valueId"];
echo $xml->enumValue["value"];

EDIT:

I would approach your foreach loop differently

foreach($xml->parameter->enumValue as $key=>$param):
   echo $param['valueId'];
   echo $param['value'];
endforeach;
Sign up to request clarification or add additional context in comments.

3 Comments

I am guiding you to the answer. What have you tried with the code I provided? "Nothing is changed." is non-descriptive.
still nothing is changed. I think there is a problem with <parameter id="9" name="Šperk" typ="ENUM" systemid="s9"> <enumValue valueId="4" value="Male"/> because there is twice tag with parameter.... ???
Sorry, I overlook your comment. Im uploading it to net, apps.crossroad.sk/zdenka/importopt.php and when I did something wrong, it doesnt show me an eror. So I think there is a problem with <parameter id="9" name="Šperk" typ="ENUM" systemid="s9"> <enumValue valueId="4" value="Male"/> because there is twice tag with parameter.... ???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.