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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:29:51+00:00 2026-05-17T20:29:51+00:00

I have a merged xml with a root element and multiple item child elements.

  • 0

I have a merged xml with a root element and multiple item child elements.
Something like this

 <root>
  <item>test1</item>
  <item>test2</item>
 </root>

What I want is an easy way to parse the xml and create an array of xml strings from the items.

$arrXml[0] = '<item>test1</item>';
$arrXml[1] = '<item>test2</item>';

I’m looking for an elegant solution not any solution.

  • 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-17T20:29:51+00:00Added an answer on May 17, 2026 at 8:29 pm

    Ok, like already mentioned in the comments to your question I am not convinced this is really what you should be doing, but since I cant stand SimpleXml and dont want people to think it’s the only way, here is how to do it

    with DOM:

    $arrXml = array();
    $dom    = new DOMDocument;
    $dom->loadXML( $xml );
    foreach( $dom->getElementsByTagName( 'item' ) as $item ) {
        $arrXml[] = $dom->saveXML( $item );
    }
    print_r( $arrXml );
    

    with XMLReader:

    $arrXml = array();
    $reader = new XmlReader;
    $reader->xml( $xml );
    while( $reader->read() ) {
        if( $reader->localName === 'item' && $reader->nodeType === 1 ) {
            $arrXml[] = $reader->readOuterXml();
        }
    }
    print_r( $arrXml );
    

    and XMLParser*:

    xml_parse_into_struct(xml_parser_create(), $xml, $nodes);
    $xmlArr = array();
    foreach($nodes as $node) {
        if($node['tag'] === 'ITEM') {
            $arrXml[] = "<item>{$node['value']}</item>";
        }
    }
    print_r($arrXml);
    

    * this can also be done via callbacks triggered when an ITEM element is encountered. Would take more code, but is pretty flexible.

    Note that all of the above might need some tweaking depending on your real XML.


    Given that the XML in your question (which is likely only an example) is dirt simple and well defined, you can also use explode():

    $arrXml = array_map( 'trim',
        array_filter(
            explode( PHP_EOL, $xml ),
            function( $line ) { return substr( $line, 0, 6 ) === '<item>'; }
    ));
    print_r( $arrXml );
    

    or a Regex (read disclaimer below)

    preg_match_all('#<item>.*</item>#', $xml, $arrXml);
    print_r($arrXml[0]);
    

    Disclaimer Now, just to make sure you are not starting to parse XML with Regex or Explode on a regular basis: The last two approaches are only feasible if your markup is really that clearly defined as you show it.

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

Sidebar

Related Questions

Question We have a large number of xml configuration files that we want merged
Is it possible to merge elements using XSLT. If I have the following XML
I have two STL containers that I want to merge, removing any elements that
I have a directory of very large XML files with a structure as this:
Is there any way to have something like svn externals for files stored in
I have an XML file which contains multiple nodes that have children. If the
We have a issue where we are trying to merge persistence.xml files from multiple
I have a dev branch that hasn't been touched for a while. I merged
I have two unix partitions under debian which I would like to merge (disk
Using xslt/xpath, I need to be able to select the two elements which have

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.