Not well-formed error at the end [duplicate] - php

This question already has answers here:
Extra content at the end of the document PHP and XML
(3 answers)
Closed 5 years ago.
As you can see I tries to make a very simple XML example with PHP. My problem is that at the end, at </users> I get an XML Parsing Error: not well-formed and I do not understand why since I do not use any special characters or any URLs. Does anyone have any ideas what the problem might be?
<?php
header('Content-type: text/xml');
$xml = "<?xml version=\"1.0\" ?>\n";
$xml .= "<users>\n";
$xml .= "test\n";
$xml .="</users>";
echo $xml;
?>

Problem solved. The solution was posted in this thread: Extra content at the end of the document PHP and XML
In short, I had to open the file with HexEdit and delete some characters.

Related

How to read xml file as it is in browser using php? [duplicate]

This question already has answers here:
How to echo XML file in PHP
(10 answers)
Closed 3 years ago.
I have created an xml file using DOMDocument. I want to read the xml file as it is in browser. If I do
$xml = simplexml_load_file('/var/lol/test.xml');
print_r($xml);
It displays the parsed object not the xml itself. I want the xml displayed as it is. How do I do this. I have tried file_get_contents() too. Doesn't work. Please help.
Use like below to show xml content as like it is on the web.
echo '<p>This is XML string content:</p>';
echo '<pre>';
echo htmlspecialchars(file_get_contents("test.xml"));
echo '</pre>';
test.xml content is like below
<?xml version="1.0"?>
<book>
<title>This is the title</title>
</book>
Use following header to display the XML:
header("Content-type: text/xml");
echo file_get_contents("/path/to/xml/file.xml");

PHP SimpleXML raw content of node [duplicate]

This question already has answers here:
PHP SimpleXML get innerXML
(11 answers)
Closed 6 years ago.
I am using PHP SimpleXML to parse data in a file. Say for example I have the following XML content:
<?xml version="1.0" ?>
<root>
<parent1>
<child1>blah</child1>
<child2>blah blah</child2>
</parent1>
<parent2>blah</parent2>
</root>
I basically want to get the actual raw content of the inside of a node. I.e. I need it to return the "innerXML" of parent1 just as text, tags and all. Having trouble getting PHP to do this. Help?
For getting the exact content '<child1>blah</child1><child2>blah blah</child2>' as itself you better put them under <![CDATA[]]> Then you can get any thing inside that as iself.
xml should be like this::
<?xml version="1.0" ?>
<root>
<parent1>
<![CDATA[<child1>blah</child1>
<child2>blah blah</child2>]]>
</parent1>
<parent2>blah</parent2>
</root>
Then
$xml = simplexml_load_string($xmlstring, 'SimpleXMLElement', LIBXML_NOCDATA);
$parent1 = $xml->parent1->asXml();
echo "<pre>";echo ($parent1);echo "<pre>";
This will output :
<child1>blah</child1>
<child2>blah blah</child2>

XML : PHP echo is empty [duplicate]

This question already has an answer here:
Get root node of XML doc using simplexml
(1 answer)
Closed 8 years ago.
Having an issue echo'ing xml tags.
PHP:
$xml = $insureFormResult->returned;
$xml1 = new SimpleXMLElement($xml);
$result = $xml1->xpath("response")[0];
echo $result;
If I echo $xml it gives me:
<?xml version="1.0" encoding="utf-8"?>
<response>
<errors>
<error code="7">Your details are already in our system and have been forwarded to our insurance partners who will contact you shortly</error>
</errors>
</response>
The xml will always have one response tag. I also want to know how to echo the tag with id 'code'. I tried the php above but there's no result echoed.
Any kind of help will be appreciated!
EDIT
It wasn't working because of the version of PHP on my server.
I'm not really familiar with PHP, but the xpath to access the error with attribute code with value 7 is:
/response/errors/error[#code='7']

How to store a XML file as a String in PHP [duplicate]

This question already exists:
How do I enable error reporting in PHP? [duplicate]
Closed 8 years ago.
Hello everyone I am trying to create a XML file as a string in php so then I can curl it up to our system for processing.
How come I can't just do this?
$XML = ' <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
All of the php code below seems to not want to be picked up anymore after this.
Why does this happen since the entire string is surrounded by ' ' ?
If there is a way around this please let me know.
Use the heredoc syntax :
<?php
$str = <<<XML
<?xml version="1.0" encoding...
<partlist>
...
</babelpart>
XML;

xml parsing with simple xml [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Problem with simpleXML and entity not being defined
I have this tag with an entity in an xml file:
<comune>Forli'</comune>
Simple xml in php fail to parse the file:
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: parser error : Entity 'igrave' not defined
How can i do?
I tried this a small example and it worked for me
XML:
<?xml version="1.0" encoding="UTF-8"?>
<comune>
<comune>Forli'</comune>
</comune>
PHP:
$xml = simplexml_load_file('test.xml');
foreach($xml->children() as $child){
echo '<pre>';
print_r((string)$child);
echo '</pre>asd';
}
OUTPUT:
Forli'
Check this solution for help

Categories