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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:11:55+00:00 2026-05-23T19:11:55+00:00

I need a way to include an external XML file in PHP which does

  • 0

I need a way to include an external XML file in PHP which does not use simplexml tags. Furthermore, I’d also need it to integrate with other imported XMLs, hence removing file headers as <?XML version...>

Basically have a PHP class which includes methods to dynamically create XML elements based on user-input. For example, I could create a node called “test”, set “id=1” as attribute and add child nodes to it. What I basically need, is a way to extract further XML content from other files and have my PHP script recognize it, hence being able to call methods on this imported code. I tried using php’s fopen() function but, although it would print the imported XML to the screen, it would not validate and signal an error as soon as the imported code began. I cannot use simpleXML extension for two main reasons. Firstly, the entire class is written using Pre-PHP5 XML handling, and I cannot re-write the whole thing from scratch as it is part of a team-project, secondly, such class features methods which could not be replicated with simpleXML extension.

This is the XML I generate: <?xml version="1.0"?> <ga><dsa>hea</dsa><sda>eh</sda></ga> <gg><ds>he</ds><sd>eh</sd></gg> And it returns: Illegal Content, Line 3 Column 1, highliting the “<” of the “gg” tag… (Which, by the way, is the part imported from the external file.)

This is a snippet of the code used to print imported XML:

$file = simplexml_load_file($url);
     foreach($file as $key => $value) {
         echo "<" . $key . ">" .  $value . "</" . $key . ">\n";
      }

How can this be done?

Additional note: Yes, the server suppors PHP 5 (5.2.6), but the code was written in pre-php5.

  • 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-23T19:11:56+00:00Added an answer on May 23, 2026 at 7:11 pm

    Judging from your comments I’d say you get an error because a valid XML document needs a root element. You XML has two: <ga> and <gg>, which means the XML is invalid and cannot be parsed.

    You should fix your XML by adding a root element. Then the parsing errors will go away.

    Another option would be to load the snippet as a document fragment with DOM:

    $brokenXML = <<< XML
    <?xml version="1.0"?>
    <ga><dsa>hea</dsa><sda>eh</sda></ga>
    <gg><ds>he</ds><sd>eh</sd></gg>
    XML;
    
    $dom = new DOMDocument;
    $fragment = $dom->createDocumentFragment();
    $fragment->appendXML(trim(str_replace('<?xml version="1.0"?>', '', $brokenXML)));
    echo $dom->saveXml($fragment);
    

    Output:

    <ga><dsa>hea</dsa><sda>eh</sda></ga>
    <gg><ds>he</ds><sd>eh</sd></gg>
    

    But note that this is still not a complete XML document because it misses a root element.

    If you want to import a DOM Tree into another, you can use DOMDocument::importNode. To use that with the fragment above, you would do

    $dom2 = new DOMDocument('1.0', 'utf-8');
    $dom2->appendChild($dom2->createElement('foo'))
            ->appendChild($dom2->importNode($fragment, true));
    
    echo $dom2->saveXml();
    

    That would result in

    <?xml version="1.0" encoding="utf-8"?>
    <foo><ga><dsa>hea</dsa><sda>eh</sda></ga>
    <gg><ds>he</ds><sd>eh</sd></gg></foo>
    

    If you have an existing document you want to import to, you would simply do

    $dom2 = new DOMDocument;
    $dom2->load('existingFile.xml');
    $dom2->documentElement->appendChild($dom2->importNode($fragment, true));
    

    This would append the fragment as the last child of the root node. If you want to have it somewhere else on the DOM tree, you would have to traverse the DOM tree with Xpath or getElementsByTagName or getElementsById or the childNodes property on the various nodes and then append to that node instead.

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

Sidebar

Related Questions

I need a way to bind POJO objects to an external entity, that could
Need a way to allow sorting except for last item with in a list.
I need a way to easily export and then import data in a MySQL
I need a way to recursively delete a folder and its children. Is there
I need a way to update the month value on a dateTime field in
I need a way to build C++ code from UML diagrams and vice versa.
I need a way to represent a 2-D array (a dense matrix) of doubles
I need a way to allow each letter of a word to rotate through
I need a way to determine the type of an HTML element in JavaScript.
I need a way to determine whether the computer running my program is joined

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.