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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:52:54+00:00 2026-05-25T01:52:54+00:00

Im trying to figure out the most effective way to parse som quite complicated

  • 0

Im trying to figure out the most effective way to parse som quite complicated XML using SimpleXML, and I’ve become stuck when there are namespaces in the document.

Okay, so my XML looks something like this:

<ns:event xmls:ns="http://example.com/event/1.1">
   <ns:eventinfo>
       <ns:start year="2011" month="9" />
       <ns:eventnames>
           <ns:eventname>Superevent</ns:eventname>
       </ns:eventnames>
   </ns:eventinfo>
   <ns:eventlocale>My place</ns:eventlocale>
</ns:event> 

I am able to extract the information from the “normal” tags via:

$data = simplexml_load_string($xml);
foreach ($data->children('ns', true) as $children) {
    $child = $children->children('ns',true);
    $eventname = ($child->eventname);
}

This would give $eventname as Superevent. However, this approach doesn’t work with attributes…

But if there wouldn’t be any namespaces, I would easily extract the attributes with for example:

$startyear = $data->$start['year'];

So – anyone got an idea to approach this problem with ease? Any info or ideas would be much appreciated.

  • 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-25T01:52:56+00:00Added an answer on May 25, 2026 at 1:52 am

    Have you considered using SimpleXML’s xpath functionality?

    // your data example missed an 'n' in xmlns, so reposted, just to be sure
    // with some additional ns:event elements in a root element
    $xml = '<?xml version="1.0" encoding="utf-8"?>
    <root>
        <ns:event xmlns:ns="http://example.com/event/1.1">
           <ns:eventinfo>
               <ns:start year="2011" month="9" />
               <ns:eventnames>
                   <ns:eventname>Superevent</ns:eventname>
               </ns:eventnames>
           </ns:eventinfo>
           <ns:eventlocale>My place</ns:eventlocale>
        </ns:event>
        <ns:event xmlns:ns="http://example.com/event/1.1">
           <ns:eventinfo>
               <ns:start year="2011" month="8" />
               <ns:eventnames>
                   <ns:eventname>Another Superevent</ns:eventname>
               </ns:eventnames>
           </ns:eventinfo>
           <ns:eventlocale>Your place</ns:eventlocale>
        </ns:event>
    </root>';
    
    $data = simplexml_load_string($xml);
    $data->registerXPathNamespace( 'ns', 'http://example.com/event/1.1' );
    
    # '//ns:event' means: find ns:event elements anywhere in the document
    # '/ns:event' would mean: find ns:event elements that are direct children of the root
    $events = $data->xpath( '//ns:event' );
    foreach( $events as $event )
    {
    
        # from now on, we are using $event (ns:event elements) as our contexts to query
    
        # '.' means: from our current context node find...
        # '/ns:eventinfo[1]' means: find the first ns:eventinfo element that is a direct child of the preceding expression
        # '/ns:eventnames[1]' means: same as previous but ns:eventnames element
        # '/ns:eventname[1]' means: same as previous but ns:eventname element
        $eventname   = $event->xpath( './ns:eventinfo[1]/ns:eventnames[1]/ns:eventname[1]' );
    
        # '/@year' means: the year attribute that is a direct child of the preceding expression
        $year        = $event->xpath( './ns:eventinfo[1]/ns:start[1]/@year' );
    
        # '/@month' means: same as previous but month attribute
        $month       = $event->xpath( './ns:eventinfo[1]/ns:start[1]/@month' );
    
        $eventlocale = $event->xpath( './ns:eventlocale[1]' );
    
        # echo the first elements from the results found by our xpath queries
        echo 'Event "' . $eventname[ 0 ] . '" taking place at ' . $eventlocale[ 0 ] . ' sometime in ' . $month[ 0 ] . '/' . $year[ 0 ] . '<br>';
    }
    

    edit:

    What I did in the previous example was rather verbose by the way. This should probably work just as well inside the loop, as you are taking the first found nodes anyway already in the echo statement:

    # relative to our context node '.' find all descendants '//' that is an ns:eventname element
    $eventname   = $event->xpath( './/ns:eventname' );
    # relative to our context node '.' find all descendants '//' that is a year attribute of an ns:start element
    $year        = $event->xpath( './/ns:start/@year' );
    # relative to our context node '.' find all descendants '//' that is a month attribute of an ns:start element
    $month       = $event->xpath( './/ns:start/@month' );
    # relative to our context node '.' find all children '/' that is an ns:eventlocale element
    $eventlocale = $event->xpath( './ns:eventlocale' );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using php I'm trying to figure out the most efficient way to design my
I am trying to figure out the most efficient / effective way of performing
I am trying to figure out the most efficient way to ensure cross-browser compatibility.
I'm trying to figure out the most efficient way to implement RoR-style partials/collections for
I'm trying to figure out the most efficient way to running a pretty hefty
I'm trying to figure out the most elegant way to change the color of
Like most users, I'm simply trying to figure out a secure way to store
I am trying to figure out the most efficient way to format this query.
Problem I am trying to figure out the most reliable way to determine the
I'm just learning Haskell, and trying to figure out the most idiomatic way to

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.