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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:13:36+00:00 2026-05-26T08:13:36+00:00

I’m using XMLReader to parse a file which i don’t control. The file is

  • 0

I’m using XMLReader to parse a file which i don’t control. The file is missing some elements, example below. Where book 2 is missing an info element, i still want to add this to the array. I realise i could use a combination of XMLReader and SimpleXML… but i’d like to see if this is possible?

<book>
    <title>book 1</title>
    <info>book 1 info</info>
</book>
<book>
    <title>book 2</title>
</book>
<book>
    <title>book 3</title>
    <info>book 3 info</info>
</book>

I have the following XMLReader function:

<?php
function parseXML($args){
    extract($args);
    $xml = new XMLReader();
    $xml->open($file) or die('Cannot open file');

    while($xml->read()){
        switch($xml->nodeType){
            case(XMLREADER::ELEMENT):{
                if(in_array($xml->localName, $nodes, true)){
                    $lName = $xml->localName;
                    $val = $xml->readString();
                    $xml->read();

                    $child[$lName] = trim($val);
                    if($lName == end($nodes)){
                        $arr[] = $child;
                    }
                }
            }
            break;
        }
    }
    $xml->close($file);
    return $arr;
}
$settings = array(
    'file' => 'books.xml',
    'nodes' => array('name', 'info')
);
$books = parseXML($settings);
var_dump($books);
?>

which produces:

Array
(
[0] => Array
    (
        [name] => book 1
        [info] => book 1 info
    )

[1] => Array
    (
        [name] => book 3
        [info] => book 3 info
    )
)
  • 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-26T08:13:37+00:00Added an answer on May 26, 2026 at 8:13 am

    You could rethink your logic a little. The snippet below builds up a $book array for each <book> element, starting each time with default values (an empty string) in case a <name> or <info> does not exist for the book.

    As <name> and <info> elements are reached, their contents are added to the current $book array.

    When reaching a </book>, the $book array is added to the main array of $books.

    Building $books

    function parseXML($args){
        extract($args);
    
        $xml   = new XMLReader();
        $xml->open($file) or die('Cannot open file');
    
        $books = array();
    
        while ($xml->read()) {
            // <book> - start a new child array with default values
            if ($xml->nodeType === XMLReader::ELEMENT 
             && $xml->localName === 'book') {
                $book = array_fill_keys($nodes, '');
            // </book> - add the child array to main array
            } elseif ($xml->nodeType === XMLReader::END_ELEMENT 
                   && $xml->localName === 'book') {
                $books[] = $book;
            // <info> or <title> - add to child array
            } elseif ($xml->nodeType === XMLReader::ELEMENT 
                   && in_array($xml->localName, $nodes, TRUE)) {
                $name = $xml->localName;
                $val  = $xml->readString();
                $book[$name] = trim($val);
            }
        }
    
        $xml->close();
        return $books;
    }
    

    Resulting array

    $books = array(
      array('title' => 'book 1', 'info' => 'book 1 info'),
      array('title' => 'book 2', 'info' => ''),
      array('title' => 'book 3', 'info' => 'book 3 info'),
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.