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

  • Home
  • SEARCH
  • 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 3695864
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:40:10+00:00 2026-05-19T04:40:10+00:00

This is my XML file, and i want to know how to get the

  • 0

This is my XML file, and i want to know how to get the ID of the article when you know
what the title is. Is this possible using Xpath?

XML file:

<Articles>
    <Article ID="1">
        <title>test</title>
        <content>test</content>
    </Article>
</Articles>

Php code:

$dom = new DOMDocument;
$dom->load("Articles.xml");
$xp = new DOMXPath($dom);
$id = $xp->evaluate('string(/Articles/Article[title="test"]/@ID)');
var_dump($id);

Kind regards,

User.

  • 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-05-19T04:40:10+00:00Added an answer on May 19, 2026 at 4:40 am

    As long as you are using SimpleXml, you cannot return the ID directly. The correct XPath would be

    /Articles/Article[title="crack"]/@ID
    

    as shown elsewhere already. But SimpleXml has no concept of Attribute nodes. It only knows SimpleXmlElements. So you can just as well fetch the Article node instead

    /Articles/Article[title="crack"]
    

    because in the end you’ll have to do the following to get the ID value anyway:

    echo $articles[0]['id'];
    

    Note that you do not need the @ when accessing the attribute from the SimpleXml Element.

    If you wanted to fetch the ID attribute directly, you’d have to use DOM (codepad)

    $dom = new DOMDocument;
    $dom->loadXml($xml);
    $xp = new DOMXPath($dom);
    $id = $xp->evaluate('string(/Articles/Article[title="crack"]/@ID)');
    var_dump($id);
    

    The above would return 1 as long as there is no other node with a title element with a value equal to "crack".

    In case you have nodes with formatting, like shown in your screenshot, you have to sanitize the element before testing for the value. Otherwise the whitespace used to format the document is taken into account. The XPath for that would be

    string(/Articles/Article[normalize-space(title) = "crack"]/@ID)
    

    If you want to search for articles with a title that contains "crack" in part, use

    string(/Articles/Article[contains(title, "crack")]/@ID)
    

    Note that this will still only return the ID immediately as long as there is just one result. If there is multiple matching nodes, the first ID value will be returned. Use

    /Articles/Article[contains(title, "crack")]/@ID
    

    to return a DOMNodeList of all matching nodes and then iterate over them to get their individual ID values.

    Also see the basic usage examples for SimpleXml in the PHP Manual

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

Sidebar

Related Questions

this is my XML file <atm> i want to know how can i verify
I want to parse a xml file using xquery in Ruby, I found this
Im new to this.I have a cron file called xml.php and i want to
Possible Duplicate: Creating a simple XML file using python I want to write a
This is my Xml file.i want to transform this xml file into another customized
I have thread where downloading xml file a this xml file i want to
Hi I can't understand this error I want read this XML file: <?xml version=1.0
This is my xml file. I want to add a new element to all
I want to read an xml file placed in assets/ directory. I've tried this
I have a on memory Datatable like this. I want to produce xml file

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.