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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:14:41+00:00 2026-06-14T15:14:41+00:00

The xml data looks like this: <feed> <entry> <id>12345</id> <title>Lorem ipsum</title> <link type=type1 href=https://foo.bar

  • 0

The xml data looks like this:

<feed>    
    <entry>
      <id>12345</id>
      <title>Lorem ipsum</title>
      <link type="type1" href="https://foo.bar" />
      <link type="type2" href="https://foo2.bar"/>
    </entry>
    <entry>
      <id>56789</id>
      <title>ipsum</title>
      <link type="type2" href="https://foo4.bar"/>
      <link type="type1" href="https://foo3.bar" />
    </entry>
</feed>

I want to select the content of the href attribute from a link with certain type. (note that type 1 is not always the first link)

Part of the code that works:

for($i=0; $i<=5; $i++) {
    foreach($xml->entry[$i]->link as $a) {
        if($a["type"] == "type2")
            $link = (string)($a["href"]);
    }
}

However, I wonder if there is a faster and more elegant solution to this that does not require a foreach loop. Any ideas?

  • 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-14T15:14:44+00:00Added an answer on June 14, 2026 at 3:14 pm

    Have you tried using xpath? http://php.net/manual/en/simplexmlelement.xpath.php

    This will allow you to do a search for nodes with the specified tag/attribute.

    $nodes = $xml->xpath('//link[@type="type2"]');
    foreach ($node in $nodes)
    {
        $link = $node['href'];
    }
    

    // Updated

    You can skip the for loop if you are interested in only the first value. The xpath function returns an array of SimpleXmlElement objects so you can use index 0 to retrieve the first element, and then it’s property.

    Note – If the element is missing or cannot be found, the xpath element will return false, and the below code will error. The code is for illustration only, so you should verify error checking when implementing it.

    // This will work if the xml always has the required attrbiute - will error if it's missing
    $link = $xml->xpath('//link['@type="type2"]')[0]['href'];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My XML Feed looks like this http://tinyurl.com/6wc6fel Not sure how to read the data
I have some data in an XML element that looks like this: <?xml version=1.0
I have XML data that looks like this <?xml version=1.0 encoding=utf-8?> <root> <account var1=
I've been trying to access some XML, which looks like this: <feed xmlns:s=http://syndication.nhschoices.nhs.uk/services xmlns=http://www.w3.org/2005/Atom>
My XML document looks like this- <doc> <system_data> <registry_item id=1> <hive>HKEY_LOCAL_MACHINE</hive> </registry_item> <file_item id=2>
Our C++ application reads configuration data from XML files that look something like this:
My existing xml file looks like <transaction> <id> <in>computer</in> <sn>1234567</sn> <book>JAVA</book> <author>klen</author> </id> <data>
I'm working with XML data from an application where we get XML like this:
I'm trying to parse data from Archive.org's search functionality. The data looks like this:
My data looks like this: 00000000001 : `12341234...12341234' Basically a unique id value associated

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.