1

When I try to Import xml file, it returns an error:

xmlParseCharRef: invalid xmlChar value 4" because of $amp; in xml <STOCKGROUP >NAME="ABC &amp; Glass" RESERVEDNAME="">

Here is code I'm using to read xml file data

function add_product_type()
{
    print_r($_FILES);
    if (isset($_FILES['product_type_file']) && ($_FILES['product_type_file']['error'] == UPLOAD_ERR_OK)) {
        $use_errors = libxml_use_internal_errors(true);
        $response = simplexml_load_file($_FILES['product_type_file']['tmp_name']);
        print_r($response);
        foreach($response->BODY->IMPORTDATA as $key => $value) {
            foreach($value->REQUESTDATA->TALLYMESSAGE as $key => $values) {
                if (strstr("&amp;", $values->STOCKGROUP->attributes())) {
                    $name = str_replace("&amp;", "&", $values->STOCKGROUP->attributes());
                }
                else {
                    $name = $values->STOCKGROUP->attributes();
                }

                echo $name . ",";
            }
        }

        if ($response == false) {
            echo "Failed loading XML\n";
            foreach(libxml_get_errors() as $error) {
                echo "\t", $error->message;
            }
        }
    }
}
4
  • IT sounds like the XML you're trying to read has a syntax error. Can you post an example document? Commented Feb 28, 2018 at 9:09
  • xmlParseCharRef: invalid xmlChar value 4 this error occurs Commented Feb 28, 2018 at 9:14
  • No, post a complete example of the XML data you're trying to parse. The XML in the error message looks very broken, but since it's a fragment it's not possible to tell. Commented Feb 28, 2018 at 9:18
  • drive.google.com/file/d/16aI_-TE3TGb6N4K58mA2w9I3DLiuyeFQ/… Commented Feb 28, 2018 at 9:30

2 Answers 2

1

XML has no notion of HTML entities. As a hack, you can decode the entities first with

$html = html_entity_decode($file_contents, ENT_QUOTES, "utf-8");

and then try parse $html with the XML parser. Just hope it's tolerant enough, because HTML is still not valid XML.

The good news is that you can then remove the if (strstr("&amp;", hack because that is taken care of by html_entity_decode().

Sign up to request clarification or add additional context in comments.

Comments

0

Your XML sample isn't the same data as your getting the error with and processes OK, but this doesn't mean that your code is correct. In your main loop, you use $values->STOCKGROUP->attributes() as the name, but if you did a print_r() of this value, you will see it comes out with a list of all of the attribute values and your code will combine them all into one string.

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [NAME] => Alluminium Section & Glass
            [RESERVEDNAME] => 
        )

)

If you just want the NAME attribute of <STOCKGROUP NAME="Alluminium Section &amp; Glass" RESERVEDNAME="">, then use STOCKGROUP['NAME']. Also rather than just substitute &amp;, use html_entity_decode() to convert any characters in the name.

foreach($response->BODY->IMPORTDATA as $key => $value) {
    foreach($value->REQUESTDATA->TALLYMESSAGE as $key => $values) {
        $name = html_entity_decode((string)$values->STOCKGROUP['NAME']);    
        echo $name . ",";
    }
}

This code with the sample you provide outputs...

Alluminium Section & Glass,

Update: Check the file being loaded...

$data = file_get_contents($_FILES['product_type_file']['tmp_name']);
echo $data;
$use_errors = libxml_use_internal_errors(true);
$response = simplexml_load_string($data);
echo $response->asXML();

14 Comments

print_r($response) How to print SimpleXMLElement Object ( [@attributes] => Array ( [NAME] => Alluminium Section & Glass [RESERVEDNAME] => ) )
If you want to print a SimpleXMLElement - use echo $response->asXML();
when i use echo $response->asXML(); => give me this error ->Call to a member function asXML() on boolean in
This means the XML isn't correct - BUT the sample data you gave works, so this either isn't the same data or isn't the complete data.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.