Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9219555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:08:34+00:00 2026-06-18T03:08:34+00:00

I have the following PHP and XML: $XML = <<<XML <items> <item id=12> <name>Item

  • 0

I have the following PHP and XML:

$XML = <<<XML
<items>
    <item id="12">
        <name>Item A</name>
    </item>
    <item id="34">
        <name>Item B</name>
    </item>
    <item id="56">
        <name>Item C</name>
  </item>
</items>
XML;


$simpleXmlEle = new SimpleXMLElement($XML);

print_r($simpleXmlEle->xpath('./item[1]'));
print "- - - - - - -\n";
print_r($simpleXmlEle->xpath('./item[2][@id]'));
print "- - - - - - -\n";
print_r($simpleXmlEle->xpath('./item[1]/name'));

I am able to access the ID like this

$simpleXmlEle->items->item[0]['id']

As it is a dynamic application the xpath is provided at runtime as a string so I believe I should use xpath.

The above PHP produces:

PHP:

Array
(
    [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [id] => 12
                )

            [name] => Item A
        )

)
- - - - - - -
Array
(
    [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [id] => 34
                )

            [name] => Item B
        )

)
- - - - - - -
Array
(
    [0] => SimpleXMLElement Object
        (
        )

)

I understand the 1st output, but inside the 2nd output the whole element is being returned instead of just the attribute.
1) Any ideas why?

Also the last item is empty
2)why is this and what would be the right xpath?

I am aiming for 2nd and 3rd output as: 34 (value of id attribute of 2nd element) Item A (just name of 1st element).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:08:35+00:00Added an answer on June 18, 2026 at 3:08 am

    See the below:

    // name only
    $name = $simpleXmlEle->xpath("./item[1]/name");
    echo $name[0], PHP_EOL;
    
    // id only
    $id = $simpleXmlEle->xpath("./item[2]/@id");
    echo $id[0], PHP_EOL;
    

    Prints:

    Array ( [0] => SimpleXMLElement Object ( [0] => Item A ) )
    Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 34 ) ) )
    

    Make sure you DO NOT do:

    print_r($objSimpleXML->xpath("//item[1]/name"));
    

    According to the documentation // returns ALL elements with this name, so if there is an item element on a deeper level then its value is returned too which is not be desired.

    hope that helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have added following lines at start of %PRPOGRA~%\Notepad++\plugin\APIs\php.xml <KeyWord name=$_GET></KeyWord> <KeyWord name=$_POST></KeyWord> <KeyWord
I have the following jqGrid script: jQuery(#editgrid).jqGrid({ url: editing.php?q=1, datatype: xml, // ... Where
I have XML in the following form that I want to parse with PHP
I have a line of PHP code that does the following: $xml = <xml><request>...[snipped
I have an XML file with data stored like this: <myxml> <item name=column18>88744544</item> <item
I am having trouble on my php simple xml object. I have the following
I have the following HTTP data: POST /forum/post.php HTTP/1.1 Accept: text/xml, text/html, text/plain, */*
I have the following code to read in an XML file: $xml2 = simplexml_load_file('http://www.facebook.com/feeds/page.php?format=rss20&id=334704593230758');
I have: $XML = new DOMDocument(); $XML->load('demo.xml'); $xpath = new DOMXpath($XML); $elements = $xpath->evaluate($_GET[xpath]);
Possible Duplicate: PHP XML how to output nice format I have the following PHP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.