0

Im trying to get the catId value. But i can see only the category value.

My xml file looks below:

<sample>
    <Item ItemNumber="00000088" FormattedItemNumber="00000-088">
      <CompatibleModels />
        <Category CatId="160" >  test 123 </Category>
      <Images />
      <Documents />
      <RequiredItems />
    </Item>
</sample>

$xml = simplexml_load_file("test.xml");
print_r($xml);

[sample] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [ItemNumber] => 00000088
                            [FormattedItemNumber] => 00000-088
                        )

                    [Category] =>   Bags/Luggage 123 
                )

how can get the CatId value? Why the cateId value is missing?

2

3 Answers 3

1

You can do it by many ways. Let's try-

foreach ($xml as $items) {
    echo $items->Category['CatId'];
}

WORKING DEMO: https://3v4l.org/Onqe2

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

Comments

1

print_r doesn't really work with SimpleXML objects. But from the sample data you have provided you can simply access the CatId attribute using

echo $xml->Item->Category['CatId'];

Comments

1

You can loop and get it by using following snippet, please refer inline documentation for explanation

$xml1 = simplexml_load_file("test.xml") or die("Error: Cannot create object");
foreach ($xml1->children() as $items1) { // children mean item
    echo ($items1->category['catid']); // for category tag get catid attribute
}

Comments

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.