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 8007169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:44:28+00:00 2026-06-04T17:44:28+00:00

<Node name=node1> <x x=x/> <x x=xxx/> </Node> <Node name=node2> <x x=xx/> <x x=xxxx/> </Node>

  • 0
<Node name="node1">
  <x x="x"/>
  <x x="xxx"/>
</Node>

<Node name="node2">
  <x x="xx"/>
  <x x="xxxx"/>
</Node>

Okay, this is an XML sample:

I call its content using simplexml_load_file

and if i wanna call a specific node content it should be :

$xml = simplexml_load_file('file.xml');
$contents = $xml->node1

and to call node object

$contents = $xml->node1->x

Seems fine till now,
the question is,
how to call object “x” without mentioning node name

you noticed the sample i added above it has 2 nodes

to call x from it i should mention nodename then x

i want to call all “x” from the whole XML

how is it possible?

  • 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-04T17:44:30+00:00Added an answer on June 4, 2026 at 5:44 pm

    SimpleXMLElement::xpath() will do the trick when you don’t have namespaces to worry about. If you do have namespaces… you’ll need SimpleXMLElement::registerXPathNamespace().

    <?php
    $xml = <<<EOT
    <?xml version="1.0" encoding="UTF-8" ?>
    <root>
    <Node name="node1">
      <x x="x"/>
      <x x="xxx"/>
    </Node>
    
    <Node name="node2">
      <x x="xx"/>
      <x x="xxxx"/>
    </Node>
    </root>
    EOT;
    
    // Original question
    $s = new SimpleXMLElement($xml);
    $r = $s->xpath('//Node/x');
    var_dump($r);
    
    // Something about names
    $r2 = $s->xpath('Node[@name]');
    $names = array();
    foreach($r2 as $elem){
      $names[] = "{$elem['name']}";
    }
    var_dump($names);
    
    // RoseWarad's answer (more efficient than "something about names")
    $r3 = $s->xpath('*/@name');
    var_dump($r3);
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I need to define a node name and its CDATA content using variables.
This is my XML file <employee> <name ref=a1 type=xxx></name> <name ref=a2 type=yyy></name> <name ref=a3
i would like convert xml, that: tag is the name of current node (this,
I have to read the xml node name from the following XML, but I
this is a b-day reminder code utilizing linkedlists typedef struct node { char name[61];
I need to get XmlNodeList where node name contains mystring XML <?xml version=1.0 encoding=utf-8?>
I want to output something similar to the following using XSLT XML: <myroot> <node1>
How to delete a string of the XML node using java. I have two
I've got an XML file that looks like this: ... <body> <unit id=1 name
Is it possible to extract the node name using apache digester? So, if the

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.