programing

xml을 php 파일로 로드하는 동안 'xmlParseEntityRef: no name' 경고가 발생했습니다.

goodjava 2023. 1. 15. 16:54

xml을 php 파일로 로드하는 동안 'xmlParseEntityRef: no name' 경고가 발생했습니다.

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/projects& 로드를 test.filename의 변수로 합니다.index.php에 대한 쓰기 권한이 없습니다.)

XML이 유효하지 않을 수 있습니다.

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

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

"&"를 삭제하고 HTML 코드 버전으로 바꿉니다.해 보세요.

여기서 이걸 찾았어요...

문제:XML 파서가 "xmlParseEntityRef: noname" 오류를 반환합니다.

원인: XML 텍스트의 어딘가에 &(앰퍼샌드 문자)가 표시되어 있습니다.를 들어 텍스트와 기타 텍스트가 있습니다.

솔루션:

  • 해결책 1: 앰퍼샌드를 제거합니다.
  • 해결책 2: 앰퍼샌드를 인코딩합니다(즉,&으로 특징짓다.&amp;XML 텍스트를 읽을 때는 반드시 디코딩하십시오.
  • 해결책 3: CDATA 섹션을 사용합니다(CDATA 섹션 내의 텍스트는 파서에 의해 무시됩니다). 예: <![CDATA[일부 텍스트와 일부 텍스트]>

주의: '&' '< >'는 올바르게 처리하지 않으면 모두 문제가 발생합니다.

먼저 다음 기능을 사용하여 HTML을 청소해 보십시오.

$html = htmlspecialchars($html);

특수 문자는 보통 HTML에서 다르게 표시되므로 컴파일러에 혼란을 줄 수 있습니다.맘에 들다&된다&amp;.

문제

  • PHP 함수simplexml_load_file구문 분석 오류 발생parser error : xmlParseEntityRefURL에서 XML 파일을 로드하려고 합니다.

원인

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

우리가 통제할 수 없는 일

  • 이상적으로는 유효한 XML이 PHP에 공급되는지 확인해야 합니다.simplexml_load_fileXML 작성 방법을 제어할 수 없는 것 같습니다.
  • 또한 강제할 수 없습니다.simplexml_load_file비활성 XML 파일을 처리합니다.XML 파일 자체를 수정하는 것 외에 많은 옵션이 남아 있지 않습니다.

생각할 수 있는 해결책

유효하지 않은 XML을 유효한 XML로 변환합니다.를 사용하여 변환할 수 있습니다.상세한 것에 대하여는, 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);

주의.

개발자는 잘못된 XML을 유효한 XML(theady에 의해 생성됨)과 비교하여 theady 사용 후 부작용이 없는지 확인해야 합니다.정돈은 올바르게 수행하는 데 매우 효과적이지만, 시각적으로 보고 100% 확신하는 것이 나쁠 것은 없습니다.우리의 경우 $xml과 $tidy를 비교하는 것 만큼 간단해야 합니다.

XML이 잘못되었습니다.

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

CDATA는 W3C에 따라 모든 특수 XML 문자로 묶어야 합니다.

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

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

이것은 실제로 캐릭터가 데이터를 만지작거리기 때문이다.「」를 사용합니다.htmlentities($yourText)(xml 문서 안에 html 코드가 들어있었습니다.http://uk3.php.net/htmlentities 를 참조해 주세요.

이것으로 문제가 해결됩니다.

$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))));

오픈카트에서 이 문제가 발생하는 경우 편집해 보십시오.

catalog/controller/controller/feed/google_sitemap.php 상세 및 방법 참조: xmlparseentityref-no-name-error

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