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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:03:04+00:00 2026-05-31T04:03:04+00:00

My goal is: first, find a specific key in an XML file. Second, return

  • 0

My goal is: first, find a specific key in an XML file. Second, return the parent of the key. In the code example below, the key is a filename.

Code:

<?php
    $inputXML = simplexml_load_file("data.xml");

    $myProject = lookupProject($inputXML, "file1");
    echo $myProject->projectname; //print the name of the project containing <filename>"file1"</filename>
    echo "\n";

    /*
     Goal: return the <project> that is the parent of <filename>$input_filename</filename>
     Assume: a <filename> appears in at most one <project> subtree.
     */
    function lookupProject($myXML, $input_filename)  
    {
        foreach($myXML->project as $curr_project) //notice I don't mention the root <projects></projects>
        {
            foreach($curr_project->filename as $curr_filename)
            {
                if ($curr_filename == $input_filename) 
                {
                    return $curr_project; 
                }
            }
        }
        return null; //if not found, return null
    }  
?>

Example data file, data.xml

<projects>
    <project>
        <projectname>project1</projectname>
        <filename>file1</filename>
        <filename>file2</filename>
    </project>
    <project>
        <projectname>project2</projectname>
        <filename>file3</filename>
    </project>
</projects>

If we have a more complex XML structure with many levels of subtrees, finding the parent of file1 could require lots of foreach() loops. Is there a SimpleXML command that would abstract away the loops that appear in lookupProject?

tl;dr is there a short/elegant (one-line?) solution that implements lookupProject()?

Note that this is homework, but the above question is not at all the crux of the homework problem. I’m done with the homework assignment, but knowing the answer to the above question would help me to make my solution more elegant.

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

    The more appropriate term for “key” would be “TextNode value”. You are looking for

    • SimpleXMLElement::xpath — Runs XPath query on XML data

    In your case this XPath query will give the projectname element

    /projects/project[filename="file1"]/projectname
    

    Since this is tagged homework, I’ll leave it up to you to figure out the necessary PHP code.

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

Sidebar

Related Questions

My goal is to find the package (as string) of a Java source file,
Couple questions here: My end goal is to password protect the file logged_in.php. Note:
Let me first describe my goal: I have created an object with 3 properties:
goal: I have the string 1234432144 I want to only replace the first 2
First off, let me define the end goal: I'd like to Wordpress (version 2.8)
First of all, to clarify my goal: There exist two programs written in C
I have this function. And its goal is to take the last character first
I've got some example Python code that I need to mimic in C++. I
My goal here is to find all possible combinations that sums to a given
As a goal for my first Android application to learn the Android SDK, I

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.