0

My XML:

<?xml version="1.0" encoding="utf-8" ?>
<LanguagePack>
    <Language LanguageID="0" LanguageString="test0" />
    <Language LanguageID="1" LanguageString="test1" />
    <Language LanguageID="2" LanguageString="test2" />
</LanguagePack>

How i can extract LanguageString where LanguageID = 'something'?

$myLanguageID = 2; $LanguageStringExtractbyID[$mynumber];

I don't have any code because i have no idea to do this...

Thank you!

MeTa
  • 89
  • 1
  • 3
  • 11
  • check here: http://stackoverflow.com/questions/6578832/how-to-convert-xml-into-array-in-php – Jibon Nov 13 '15 at 11:08
  • Your XML isn't valid. Can you write full xml code? – Jibon Nov 13 '15 at 11:13
  • I edited the post, Thank you – MeTa Nov 13 '15 at 11:17
  • You might want to read [How do I ask a good question](http://stackoverflow.com/help/how-to-ask), which enhances the probability for getting a useful answer _drastically_. You might find [ESR](https://en.m.wikipedia.org/wiki/Eric_S._Raymond)'s excellent essay [How To Ask Questions The Smart Way](http://catb.org/~esr/faqs/smart-questions.html) helpful, too. – Markus W Mahlberg Nov 14 '15 at 00:13

3 Answers3

1

Thats easy with SimpleXML and XPath ( or DOMDocument ):

$xml = <<<XML
<?xml version="1.0" encoding="utf-8" ?>
<LanguagePack>
    <Language LanguageID="0" LanguageString="test0" />
    <Language LanguageID="1" LanguageString="test1" />
    <Language LanguageID="2" LanguageString="test2" />
</LanguagePack>
XML;

$xml = simplexml_load_string($xml);
$id = 1;

$value = (string) current(
   $xml->xpath(
      sprintf('Language[@LanguageID=%s]/@LanguageString', $id)
   )
);

var_dump($value);

Results in:

string(5) "test1"
nihylum
  • 542
  • 3
  • 7
0

You can do this with PHP SimpleXML. Assuming books.xml contain:

<bookstore>
  <book category="COOKING" editor="BLABLA">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
</bookstore>

php code will be:

$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");
echo $xml->book[0]['category'] . "<br>";
echo $xml->book[0]['editor'] . "<br>";
echo $xml->book[1]->title['lang']; 

This exaple is taken from W3School php tutorial

0

Try this:

$data = '<LanguagePack>
    <Language LanguageID="0" LanguageString="test0" />
    <Language LanguageID="1" LanguageString="test1" />
    <Language LanguageID="2" LanguageString="test2" />
</LanguagePack>';


$xml = simplexml_load_string($data, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

foreach ($array['Language'] as $language){
    //print_r($language['@attributes']);
    // Do Stuff
}
Jibon
  • 232
  • 6
  • 26