PHP XML parsing created in PHP

I am trying to parse an XML string with NSXMLParser that I created in PHP. My PHP sript:

$doc = new DomDocument('1.0');
//$doc->preserveWhiteSpace=false;
$root = $doc->createElement("root");
$root = $doc->appendChild($root);
$child = $doc->createElement("child");
$child = $root->appendChild($child);
$value = $doc->createTextNode("testvar");
$value = $child->appendChild($value);
$xml_string = $doc->saveXML();
print $xml_string;

If I open the page in a browser, it will show me "testvar" as it should. Showing the source, it shows me that there are several spaces:

begin ->| <?xml version="1.0"?>
<root><child>testvar</child></root>
    |<--end

Setting preserveWhitespace to false does not change anything. So I tried with regex in php:

$xml_string1 = preg_replace("/\s+</","<",$xml_string);
$xml_string2 = preg_replace("/>\s+/",">",$xml_string1);
print $xml_string2;

This removes \ n and leads me to this

begin-->| <?xml version="1.0"?><root><child>testvar</child></root>  |<--end

The browser always displays the desired result: testvar

On iphone, I get the following error:

**NSXMLParserErrorDomain Code = 64, Operation could not be completed.**

I do not know what the problem is with xml, I get it correctly, as in this section. Deletes \ n ....

, [parser parse] (, ), parserError. xml, , , . iPhone , UTF8 ASCII. ,

stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet

.

:

[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];

, , , .

.

+3
2

, . XML-, . , 1,5 . PHP : <?php ... ?>. , php ?> PHP-, .. PHP ( XML), . , .

+1

, NSXMLParser. , , , :

  • PHP script, XML ? , , <?php.

  • , UTF-8 'faux-BOM.

, , XML. XML 1.0 UTF-8 ( ), XML- . , , .

, LIBXML_NOXMLDECL saveXML, , . saveXML childNodes . , , , , :

$xml_string = $doc->saveXML($doc->documentElement);
+1

Source: https://habr.com/ru/post/1734387/


All Articles