your programing

xml을 PHP 파일에로드하는 동안 'xmlParseEntityRef : 이름 없음'경고

lovepro 2020. 10. 6. 18:54
반응형

xml을 PHP 파일에로드하는 동안 'xmlParseEntityRef : 이름 없음'경고


을 사용하여 PHP에서 xml을 읽고 simplexml_load_file있습니다. 그러나 xml을로드하는 동안 경고 목록이 표시됩니다.

Warning: simplexml_load_file() [function.simplexml-load-file]: <project orderno="6" campaign_name="International Relief & Development" project in /home/bluecard1/public_html/test.php on line 3    
Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /home/bluecard1/public_html/test.php on line 3    
Warning: simplexml_load_file() [function.simplexml-load-file]: http://..../index.php/site/projects/:15: parser error : xmlParseEntityRef: no name in /home/bluecard1/public_html/test.php on line 3

Warning: simplexml_load_file() [function.simplexml-load-file]: ional Relief & Development" project_id="313" client_name="International Relief & in /home/bluecard1/public_html/test.php on line 3    
Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /home/bluecard1/public_html/test.php on line 3    
Warning: simplexml_load_file() [function.simplexml-load-file]: http://..../index.php/site/projects/:15: parser error : xmlParseEntityRef: no name in /home/bluecard1/public_html/test.php on line 3

이러한 경고를 제거하려면 어떻게해야합니까?

(XML은 URL에서 생성되고 http://..../index.php/site/projectstest.php의 변수에로드됩니다. index.php에 대한 쓰기 권한이 없습니다.)


XML은 대부분 유효하지 않습니다.

문제는 "&"일 수 있습니다.

$text=preg_replace('/&(?!#?[a-z0-9]+;)/', '&amp;', $text);

"&"를 제거하고 HTML 코드 버전으로 대체합니다. 시도해보십시오.


여기 에서 찾았 습니다 ...

문제점 : XML 구문 분석기가 "xmlParseEntityRef : noname"오류를 리턴합니다.

원인 : XML 텍스트 어딘가에 '&'(앰퍼샌드 문자)가 있습니다. 일부 텍스트 및 추가 텍스트

해결책:

  • 해결 방법 1 : 앰퍼샌드를 제거합니다.
  • 해결 방법 2 : 앰퍼샌드를 인코딩합니다 (즉, '&'문자를 '& amp;'로 대체). XML 텍스트를 읽을 때 디코딩해야합니다.
  • 솔루션 3 : CDATA 섹션을 사용합니다 (CDATA 섹션 내부의 텍스트는 파서에 의해 무시됩니다.) 예. <! [CDATA [일부 텍스트 및 추가 텍스트]]>

참고 : '&' '<' '>'는 올바르게 처리되지 않으면 모두 문제가됩니다.


이 기능을 사용하여 먼저 HTML을 정리하십시오.

$html = htmlspecialchars($html);

특수 문자는 일반적으로 HTML에서 다르게 표현되며 컴파일러에게 혼동을 줄 수 있습니다. 처럼 &됩니다 &amp;.


결합 된 버전을 사용합니다.

strip_tags(preg_replace("/&(?!#?[a-z0-9]+;)/", "&amp;",$textorhtml))

문제

  • URL에서 XML 파일을로드하는 동안 PHP 함수에서 simplexml_load_file구문 분석 오류가 발생 parser error : xmlParseEntityRef합니다.

원인

  • URL에서 반환 된 XML은 유효한 XML이 아닙니다. &대신 값을 포함 합니다 &amp;. 이 시점에서 분명하지 않은 다른 오류가있을 수 있습니다.

우리가 통제 할 수없는 것들

  • 이상적으로는 유효한 XML이 PHP simplexml_load_file함수에 제공 되는지 확인해야 하지만 XML 생성 방법을 제어 할 수없는 것처럼 보입니다.
  • simplexml_load_file유효하지 않은 XML 파일 을 강제 로 처리 할 수도 없습니다 . XML 파일 자체를 수정하는 것 외에는 많은 옵션이 남지 않습니다.

가능한 해결책

잘못된 XML을 유효한 XML로 변환합니다. 을 사용하여 수행 할 수 있습니다 PHP tidy extension. 추가 지침은 http://php.net/manual/en/book.tidy.php 에서 찾을 수 있습니다 .

확장이 존재하거나 설치되었는지 확인한 후 다음을 수행하십시오.

/**
 * As per the question asked, the URL is loaded into a variable first, 
 * which we can assume to be $xml
 */
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<project orderno="6" campaign_name="International Relief & Development for under developed nations">
    <invalid-data>Some other data containing & in it</invalid-data>
    <unclosed-tag>
</project>
XML;

/**
 * Whenever we use tidy it is best to pass some configuration options 
 * similar to $tidyConfig. In this particular case we are making sure that
 * tidy understands that our input and output is XML.
 */
$tidyConfig = array (
    'indent' => true,
    'input-xml' => true, 
    'output-xml' => true,
    'wrap' => 200
);

/**
 * Now we can use tidy to parse the string and then repair it.
 */
$tidy = new tidy;
$tidy->parseString($xml, $tidyConfig, 'utf8');
$tidy->cleanRepair();

/**
 * If we try to output the repaired XML string by echoing $tidy it should look like. 

 <?xml version="1.0" encoding="utf-8"?>
 <project orderno="6" campaign_name="International Relief &amp; Development for under developed nations">
      <invalid-data>Some other data containing &amp; in it</invalid-data>
      <unclosed-tag></unclosed-tag>
 </project> 

 * As you can see that & is now fixed in campaign_name attribute 
 * and also with-in invalid-data element. You can also see that the   
 * <unclosed-tag> which didn't had a close tag, has been fixed too.
 */
echo $tidy;

/**
 * Now when we try to use simplexml_load_string to load the clean XML. When we
 * try to print_r it should look something like below.

 SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [orderno] => 6
            [campaign_name] => International Relief & Development for under developed nations
        )

    [invalid-data] => Some other data containing & in it
    [unclosed-tag] => SimpleXMLElement Object
        (
        )

)

 */
 $simpleXmlElement = simplexml_load_string($tidy);
 print_r($simpleXmlElement);

주의

The developer should try to compare the invalid XML with a valid XML (generated by tidy), to see there are no adverse side effects after using tidy. Tidy does an extremely good job of doing it correctly, but it never hurts to see it visually and to be 100% sure. In our case it should be as simple as comparing $xml with $tidy.


The XML is invalid.

<![CDATA[ 
{INVALID XML}
]]> 

CDATA should be wrapped around all special XML characters, as per W3C


This is in deed due to characters messing around with the data. Using htmlentities($yourText) worked for me (I had html code inside the xml document). See http://uk3.php.net/htmlentities.


This solve my problème:

$description = strip_tags($value['Description']);
$description=preg_replace('/&(?!#?[a-z0-9]+;)/', '&amp;', $description);
$description= preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $description);
$description=str_replace(' & ', ' &amp; ', html_entity_decode((htmlspecialchars_decode($description))));

If you are getting this issue with opencart try editing

catalog/controller/extension/feed/google_sitemap.php For More info and How to do it refer this: xmlparseentityref-no-name-error

참고URL : https://stackoverflow.com/questions/7604436/xmlparseentityref-no-name-warnings-while-loading-xml-into-a-php-file

반응형