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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:20:09+00:00 2026-06-14T22:20:09+00:00

This is the code i’m using to parse a remote XML: $xmlDoc = new

  • 0

This is the code i’m using to parse a remote XML:

$xmlDoc = new DOMDocument();
$xmlDoc->load("http://www.bnr.ro/nbrfxrates.xml");

$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
{
    if($item->nodeName == 'Body')
        foreach ($item->childNodes AS $body_item)
        {
            if($body_item->nodeName == 'Cube')
                foreach ($body_item->childNodes AS $cube_item)
                {                                                   
                    print " - ".$cube_item->nodeName . " : ".$cube_item->getAttribute("currency")." = ". $cube_item->nodeValue . "<br>";

                }


        }
}

If i remove the getAttribute part the script runs smoothly, but adding it causes it to return error 500.

The xml is available at the address if you want to check it out or something.

Once I activated error reporting I found the error to be: Object of class DOMText could not be converted to string

  • 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-14T22:20:11+00:00Added an answer on June 14, 2026 at 10:20 pm

    The problem is that childNodes is supplying both regular and text nodes. You can only call getAttribute() on the regular Element nodes (type 1). Check first that they are not text nodes before executing your print:

    foreach ($body_item->childNodes AS $cube_item)
    {
       // Only attempt to print the regular DOM elements                          
       if ($cube_item->nodeType == 1)
       {
         print " - ".$cube_item->nodeName . " : ".$cube_item->getAttribute("currency")." = ". $cube_item->nodeValue . "<br>";
       }
    }
    

    If in your foreach loop you were to check the nodeType of each of the childNodes, you would most likely see something similar to an alternating 3 1 3 1 3 1 for the whitespace text and regular element nodes, when you only really want the element nodes (1)

    MDN has a reference list of DOM node types.

    Incidentally, a quick verification of the fix with your full code produces:

     - Rate : AED = 0.9514<br>
     - Rate : AUD = 3.6518<br>
     - Rate : BGN = 2.3169<br>
     - Rate : BRL = 1.6787<br>
     - Rate : CAD = 3.5182<br>
     - Rate : CHF = 3.7628<br>
     - Rate : CNY = 0.5613<br>
     - Rate : CZK = 0.1790<br>
     - Rate : DKK = 0.6076<br>
     - Rate : EGP = 0.5726<br>
     - Rate : EUR = 4.5313<br>
     - Rate : GBP = 5.5990<br>
     - Rate : HUF = 1.6075<br>
     - Rate : INR = 0.0627<br>
     - Rate : JPY = 4.2601<br>
     - Rate : KRW = 0.3217<br>
     - Rate : MDL = 0.2836<br>
     - Rate : MXN = 0.2692<br>
     - Rate : NOK = 0.6169<br>
     - Rate : NZD = 2.8782<br>
     - Rate : PLN = 1.1019<br>
     - Rate : RSD = 0.0405<br>
     - Rate : RUB = 0.1127<br>
     - Rate : SEK = 0.5268<br>
     - Rate : TRY = 1.9469<br>
     - Rate : UAH = 0.4288<br>
     - Rate : USD = 3.4950<br>
     - Rate : XAU = 196.3192<br>
     - Rate : XDR = 5.3606<br>
     - Rate : ZAR = 0.3937<br>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code comes from: http://code.activestate.com/recipes/577090-file-encryption-using-stream-cipher/ import sys import random if len(sys.argv) != 4: print
This code fails on some machines: // Parse error: syntax error, unexpected '[' ...
This code : http://jsfiddle.net/bYtTG/1/ Works as I want in FF, chrome and opera, but
this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new
This code load the content of the php file with the parameter: online and
This code throws parse error, which I do not understand why. function t(){ return
This code does not seem to run well: http://jsfiddle.net/5GEru/1/ var left = 0; function
This code block the website ex: http://google.com or plus.google.com but https://google.com doesn't work e.Cancel
this code using an OLEDB source will populate the OutputColumnCollection below it, the code
This code works properly in my localhost. I am using xampp 1.7.3. but when

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.