1

I have currency output in XML like this :

<ValCurs Date="13.04.2019" name="Foreign Currency Market">
    <Valute ID="R01010">
      <NumCode>036</NumCode>
      <CharCode>AUD</CharCode>
      <Nominal>1</Nominal>
      <Name>Австралийский доллар</Name>
      <Value>46,0781</Value>
    </Valute>
    <Valute ID="R01020A">
      <NumCode>944</NumCode>
      <CharCode>AZN</CharCode>
      <Nominal>1</Nominal>
      <Name>Азербайджанский манат</Name>
      <Value>38,0295</Value>
    </Valute>
</ValCurs>

I want to get the <Name>, <Value> and <Valute> by supplying the <Valute> ID

I tried it with this foreach loop in php:

$xml = simplexml_load_string($url) or die ("error cannot create object");
$results = $xml->ValCurs;
$res=count($results);
echo " currency : $res <br><br>";
$output=array();
$keyword = array();
foreach($xml->ValCurs as $nodes){
$res=count($nodes);
    foreach ($nodes->Valute as $key =>$node) {
      $output=$node->NumCode;
      echo $output;
      echo "<br><br>";
      echo $keyword[]=substr($output,66);
      echo "<br><br>";  
    }
}
    echo"<hr>";
    echo $keyword[0];

Result is currency is 0

4
  • Do you mean given the XML string, you are after a function to extract the NAME, VALUE and VALUTE simply by supplying the ID? Commented Apr 17, 2019 at 13:57
  • Yes, that's exactly what I am asking. Commented Apr 17, 2019 at 14:01
  • I am also getting error with message "Undefined offset: 0" Commented Apr 17, 2019 at 14:03
  • @G'ulomjonMalikov You got more solutions . . . Commented Apr 17, 2019 at 14:15

3 Answers 3

1

This is as simple as:

$s = '<ValCurs Date="13.04.2019" name="Foreign Currency Market">
    <Valute ID="R01010">
      <NumCode>036</NumCode>
      <CharCode>AUD</CharCode>
      <Nominal>1</Nominal>
      <Name>Австралийский доллар</Name>
      <Value>46,0781</Value>
    </Valute>
    <Valute ID="R01020A">
      <NumCode>944</NumCode>
      <CharCode>AZN</CharCode>
      <Nominal>1</Nominal>
      <Name>Азербайджанский манат</Name>
      <Value>38,0295</Value>
    </Valute>
</ValCurs>';

// Note, that `simplexml_load_string` uses a STRING, 
// if `$url` is url to some resource, first get data from 
// this resource and then load this data to SimpleXML
$xml = simplexml_load_string($s) or die ("error cannot create object");
foreach($xml as $node){
      echo $node->Name;
      echo "<br><br>";
      echo $node->Value;
      echo "<br><br>";
      echo $node['ID'];
      echo "<br><br>";
}

Fiddle here.

Sign up to request clarification or add additional context in comments.

Comments

0
<?php
function object2array($object) { return @json_decode(@json_encode($object),1); }
$xmldata = <<<XML
<ValCurs Date="13.04.2019" name="Foreign Currency Market">
    <Valute ID="R01010">
      <NumCode>036</NumCode>
      <CharCode>AUD</CharCode>
      <Nominal>1</Nominal>
      <Name>Австралийский доллар</Name>
      <Value>46,0781</Value>
    </Valute>
    <Valute ID="R01020A">
      <NumCode>944</NumCode>
      <CharCode>AZN</CharCode>
      <Nominal>1</Nominal>
      <Name>Азербайджанский манат</Name>
      <Value>38,0295</Value>
    </Valute>
</ValCurs>
XML;
$xml = simplexml_load_string($xmldata) or die ("error cannot create object");
$xml_array=object2array($xml);
echo'<pre>';
print_r($xml_array);
foreach($xml_array['Valute'] as $key=>$value){
    echo $value['@attributes']['ID'].'<br>';
    echo $value['Name'].'<br>';
    echo $value['Value'].'<br>';
}

Sample output

R01010
Австралийский доллар
R01020A
Азербайджанский манат

4 Comments

This is prone to problems if they ever change the way SimpleXML works internally. Using object2array() isn't a good choice IMHO.
@NigelRen still this question is open to answer I'm not stoping you, can you please . . . :-)
As the answer posted by u_mulder is what I would suggest, then I have upvoted that rather than post virtually the same code.
Yes, After I saw mulder's answer I understood but I expected different answer from you that's why. . .! :-) Mutually we answered else I would been quit.
0

Steps:

1) Convert XML to string by simplexml_load_string()

2) json_encode() string for decoding purpose.

3) json_deocde(): to return as array.

4) Loop over array

5) Append loop values to array newly created.

<?php
$xml = '<?xml version = "1.0" encoding = "utf-8"?>
<ValCurs Date="13.04.2019" name="Foreign Currency Market">
    <Valute ID="R01010">
      <NumCode>036</NumCode>
      <CharCode>AUD</CharCode>
      <Nominal>1</Nominal>
      <Name>Австралийский доллар</Name>
      <Value>46,0781</Value>
    </Valute>
    <Valute ID="R01020A">
      <NumCode>944</NumCode>
      <CharCode>AZN</CharCode>
      <Nominal>1</Nominal>
      <Name>Азербайджанский манат</Name>
      <Value>38,0295</Value>
    </Valute>
</ValCurs>';

$arr = [];
$array = json_decode(json_encode(simplexml_load_string($xml)),true);
if ( ! empty($array['Valute'])) {
 $i=0;
 foreach ($array['Valute'] as $elem) {
   $arr[$i]['NumCode'] = $elem['NumCode'];
   $arr[$i]['CharCode'] = $elem['CharCode'];
   $arr[$i]['Nominal'] = $elem['Nominal'];
   $arr[$i]['Name'] = $elem['Name'];
   $arr[$i]['Value'] = $elem['Value'];
  ++$i;
 }
}
echo '<pre>';print_r($arr);echo '</pre>';

Output:

Array
(
    [0] => Array
        (
            [NumCode] => 036
            [CharCode] => AUD
            [Nominal] => 1
            [Name] => Австралийский доллар
            [Value] => 46,0781
        )

    [1] => Array
        (
            [NumCode] => 944
            [CharCode] => AZN
            [Nominal] => 1
            [Name] => Азербайджанский манат
            [Value] => 38,0295
        )

)

Working Online Code:

2 Comments

Thanks in advance!
Using json_decode(json_encode()) is a waste of time, it's simple enough using SimpleXML - why bother doing all these extra steps?

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.