1

How to get the xml sub-parameters? how to get all the results of category and return a result $category .. category = Test1, Test2

my xml

<xml>
<title>Test 123</title>
<categories>
<category>Test1</category>
<category>Test2</category>
</categories>
</xml>

my get code

$category = htmlspecialchars($item->categories->category, ENT_XML1 | ENT_QUOTES, 'UTF-8');

echo $category; //I want to return Test1, Test2
I.Nikolaew
  • 48
  • 9
  • Possible duplicate of [Access all children of a certain node with simplexml and php](https://stackoverflow.com/questions/16054386/access-all-children-of-a-certain-node-with-simplexml-and-php) – icy Jun 01 '19 at 08:35
  • No! i want to take all the subparimetres of `categories` – I.Nikolaew Jun 01 '19 at 09:40

2 Answers2

0

This is what are you searching for?

$xml = new SimpleXMLElement('<item id="1234">
<property name="country_id">
    <value>4402</value>
</property>
<property name="rc_maintenance_other">
</property>
<property name="claim_right_shareholder">
</property>
<property name="charges_other">
</property>
<property name="other_expenses_heating">
</property>
<property name="unpaid_bills_amount">
</property>
<property name="iv_person_phone">
    <value>03-6756711</value>
</property>
</item>
 ');

foreach ($xml->xpath('//item[@id="1234"]') as $item)
{    
    foreach ($item->children() as $child) {
      echo $child['name'] ."\n";
    }
}

duplicate question? Access all children of a certain node with simplexml and php

icy
  • 1,468
  • 3
  • 16
  • 36
0

You could loop $item->categories->category using a foreach:

$source = <<<DATA
<xml>
<title>Test 123</title>
<categories>
<category>Test1</category>
<category>Test2</category>
</categories>
</xml>
DATA;

$item = simplexml_load_string($source);

foreach ($item->categories->category as $elm) {
    echo $elm . PHP_EOL;
}

That will give you:

Test1
Test2

See a php demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • Hello, man I get information with `simplexml_load_file` and this script does not work with it.. Help ? – I.Nikolaew Jun 02 '19 at 04:28
  • @I.Nikolaew Can you add the script that you use to load the file to the question? Does the xml look exactly like that? I tested it like this `$item = simplexml_load_file("file.xml");` and it seems to work. – The fourth bird Jun 02 '19 at 15:05
  • My php code is ```$xml = simplexml_load_file($rss_url); foreach ($xml->offert as $item) { foreach ($item->categories->category as $category) { $category = htmlspecialchars($category, ENT_XML1 | ENT_QUOTES, 'UTF-8').','; ....``` * Test1 Test2 ...* – I.Nikolaew Jun 02 '19 at 18:16
  • @I.Nikolaew Instead of using `$xml->offert` you could use `$xml->categories->category` see https://3v4l.org/IVK1g or if you have multipe see https://3v4l.org/UK23I – The fourth bird Jun 03 '19 at 07:38
  • Here is my code: https://3v4l.org/48Ue7 as I try to do it and it does not work .. – I.Nikolaew Jun 03 '19 at 08:39
  • @I.Nikolaew I have made some minor changes to the code, it should work now https://3v4l.org/agnHX – The fourth bird Jun 03 '19 at 08:59
  • Not working see Output .. must return `Name = Title-1 || Category = Test1,Test2` not that `Name = Title-1 || Category = Test1 Name = Title-1 || Category = Test2` – I.Nikolaew Jun 03 '19 at 09:04
  • @I.Nikolaew I see what you mean, then you could use https://3v4l.org/R5gMc – The fourth bird Jun 03 '19 at 09:10
  • 1
    THANK YOU THANK YOU!! :) – I.Nikolaew Jun 03 '19 at 09:16