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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:38:01+00:00 2026-05-13T18:38:01+00:00

I’m needing to load an XML document into PHP that comes from an external

  • 0

I’m needing to load an XML document into PHP that comes from an external source. The XML does not declare it’s encoding and contains illegal characters like &. If I try to load the XML document directly in the browser I get errors like “An invalid character was found in text content” also when loading the file in PHP I get lots of warnings like: xmlParseEntityRef: no name in Entity and Input is not proper UTF-8, indicate encoding ! Bytes: 0x9C 0x31 0x21 0x3C.

It’s clear that the XML is not well formed and contains illegal characters that should be converted to XML entities.

This is because the XML feed is made up of data supplied by lots of other users and clearly it’s not being validated or reformatted before I get it.

I’ve spoken to the supplier of the XML feed and they say they are trying to get the content providers to sort it out, but this seems silly as they should be validating the input first.

I basically need to fix the XML correcting any encoding errors and converting any illegal chars to XML entities so that the XML loads problem when using PHP’s DOMDocument functions.

My code currently looks like:

  $feedURL = '3704017_14022010_050004.xml';
  $dom = new DOMDocument();
  $dom->load($feedURL);

Example XML file showing encoding issue (click to download): feed.xml

Example XML that contains chars that have not been converted to XML entities:

<?xml version="1.0"?>
<feed>
<RECORD>
<ID>117387</ID>
<ADVERTISERNAME>Test</ADVERTISERNAME>
<AID>10544740</AID>
<NAME>This & This</NAME>
<DESCRIPTION>For one day only this is > than this.</DESCRIPTION>
</RECORD>
</feed>
  • 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-13T18:38:01+00:00Added an answer on May 13, 2026 at 6:38 pm

    Try using the Tidy library which can be used to clean up bad HTML and XML
    http://php.net/manual/en/book.tidy.php

    A pure PHP solution to fix some XML like this:

    <?xml version="1.0"?>
    <feed>
    <RECORD>
    <ID>117387</ID>
    <ADVERTISERNAME>Test < texter</ADVERTISERNAME>
    <AID>10544740</AID>
    <NAME>This & This</NAME>
    <DESCRIPTION>For one day only this is > than this.</DESCRIPTION>
    </RECORD>
    </feed>
    

    Would be something like this:

      function cleanupXML($xml) {
        $xmlOut = '';
        $inTag = false;
        $xmlLen = strlen($xml);
        for($i=0; $i < $xmlLen; ++$i) {
            $char = $xml[$i];
            // $nextChar = $xml[$i+1];
            switch ($char) {
            case '<':
              if (!$inTag) {
                  // Seek forward for the next tag boundry
                  for($j = $i+1; $j < $xmlLen; ++$j) {
                     $nextChar = $xml[$j];
                     switch($nextChar) {
                     case '<':  // Means a < in text
                       $char = htmlentities($char);
                       break 2;
                     case '>':  // Means we are in a tag
                       $inTag = true;
                       break 2;
                     }
                  }
              } else {
                 $char = htmlentities($char);
              }
              break;
            case '>':
              if (!$inTag) {  // No need to seek ahead here
                 $char = htmlentities($char);
              } else {
                 $inTag = false;
              }
              break;
            default:
              if (!$inTag) {
                 $char = htmlentities($char);
              }
              break;
            }
            $xmlOut .= $char;
        }
        return $xmlOut;
      }
    

    Which is a simple state machine noting whether we are in a tag or not and if not then encoding the text using htmlentities.

    It’s worth noting that this will be memory hungry on large files so you may want to rewrite it as a stream plugin or a pre-processor.

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

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.