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/projects
test.php의 변수에로드됩니다. index.php에 대한 쓰기 권한이 없습니다.)
XML은 대부분 유효하지 않습니다.
문제는 "&"일 수 있습니다.
$text=preg_replace('/&(?!#?[a-z0-9]+;)/', '&', $text);
"&"를 제거하고 HTML 코드 버전으로 대체합니다. 시도해보십시오.
문제점 : XML 구문 분석기가 "xmlParseEntityRef : noname"오류를 리턴합니다.
원인 : XML 텍스트 어딘가에 '&'(앰퍼샌드 문자)가 있습니다. 일부 텍스트 및 추가 텍스트
해결책:
- 해결 방법 1 : 앰퍼샌드를 제거합니다.
- 해결 방법 2 : 앰퍼샌드를 인코딩합니다 (즉, '&'문자를 '& amp;'로 대체). XML 텍스트를 읽을 때 디코딩해야합니다.
- 솔루션 3 : CDATA 섹션을 사용합니다 (CDATA 섹션 내부의 텍스트는 파서에 의해 무시됩니다.) 예. <! [CDATA [일부 텍스트 및 추가 텍스트]]>
참고 : '&' '<' '>'는 올바르게 처리되지 않으면 모두 문제가됩니다.
이 기능을 사용하여 먼저 HTML을 정리하십시오.
$html = htmlspecialchars($html);
특수 문자는 일반적으로 HTML에서 다르게 표현되며 컴파일러에게 혼동을 줄 수 있습니다. 처럼 &
됩니다 &
.
결합 된 버전을 사용합니다.
strip_tags(preg_replace("/&(?!#?[a-z0-9]+;)/", "&",$textorhtml))
문제
- URL에서 XML 파일을로드하는 동안 PHP 함수에서
simplexml_load_file
구문 분석 오류가 발생parser error : xmlParseEntityRef
합니다.
원인
- URL에서 반환 된 XML은 유효한 XML이 아닙니다.
&
대신 값을 포함 합니다&
. 이 시점에서 분명하지 않은 다른 오류가있을 수 있습니다.
우리가 통제 할 수없는 것들
- 이상적으로는 유효한 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 & Development for under developed nations">
<invalid-data>Some other data containing & 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]+;)/', '&', $description);
$description= preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $description);
$description=str_replace(' & ', ' & ', 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
'your programing' 카테고리의 다른 글
Xcode 7.3은 수동 참조 계산을 사용하여 파일에 __weak 참조를 만들 수 없습니다. (0) | 2020.10.06 |
---|---|
클릭시 Google지도에 마커 추가 (0) | 2020.10.06 |
Sublime Text 2에서 Java 코드 컴파일 및 실행 (0) | 2020.10.06 |
확장 된 PHP 클래스의 정적 호출에서 클래스 이름을 어떻게 얻을 수 있습니까? (0) | 2020.10.06 |
node.js mongodb _id node-mongodb-native로 문서 선택 (0) | 2020.10.06 |