I have an XML doc that I need to load with PHP. I am currently using the simplexml_load_file() function, however the xml file is malformed, and consequently I am getting a parse error.
The XML file looks something like this:
...
</result>something1>
</else>
</else>
</resu
...
As you can see, this XML is whack and this function is throwing an error trying to parse it. Also I don't need this data that is corrupted. I would just like to read in the stuff that I can and throw everything else away.
As Jonah Bron suggested, try DOMDocument::loadHTML():
$dom = new DOMDocument();
$dom->strictErrorChecking = false;
libxml_use_internal_errors(true);
$dom->loadHTML($xml);
#Juliusz
You don't actually need to set the strictErrorChecking for this I don't think. I tried the following and it seems to work fine. To ignore the errors you need to set the libxml_use_internal_errors(true). Essentially you want to use DOMDocument instead of simplexml. I tried the following and worked without any problems:
<?php
$string = <<<XML
<?xml version='1.0'?>
<document>
<cmd>login</cmd>
<login>Richard</login>
</else>
</else>
</document>
XML;
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($string);
print $dom->saveHTML();
?>
Thusjanthan Kubendranathan
try to tidy it up, it worked well for me.
http://hu2.php.net/manual/en/intro.tidy.php
Related
I am parsing some XML with PHP DOMDocument. This is my code:
$doc = new DOMDocument;
$doc->resolveExternals = true;
$doc->substituteEntities = true;
$doc->load('../poems_xml/'.$pid.'.xml');
$xsl = new DOMDocument;
$xsl->load('../xslt/title.xsl');
$proc = new XSLTProcessor;
$proc->importStylesheet($xsl);
$ptitle = $proc->transformToXML($doc);
I have an entity file declared at the beginning of my .xml:
<?xml version="1.0" encoding="utf-8"?>
<?oxygen RNGSchema="../dtd/dps.rng" type="xml"?>
<?xml-stylesheet href="../dtd/dps.css" type="text/css"?>
<!DOCTYPE TEI SYSTEM "../dtd/entities.ent">
[...]
And the entities file looks like this:
[...]
<!ENTITY d1_AytR_002 "<rs key='d1_AytR_002'>d1_AytR_002</rs>">
[...]
In my .xml I use these entities like so:
...&d1_AytR_002;...
Now, it all goes well in terms of parsing the file and transform it via the xslt and css files, except for the entities. They just get ignored. Turning on the php_error_log flag, I get this:
Notice: DOMDocument::load(): Namespace default prefix was not found in Entity, line: 1 in index.php on line 28
(line 28 of index.php is where the load('../poems_xml/'.$pid.'.xml') instruction is). Can someone shed some light on what I should check/add regarding my entities?
I'm using PHP 5.6.40.
A workaround (and possible permanent solution) is that of adding the namespace to each of the <!ENTITY>s, like so:
<!ENTITY d1_AytR_002 "<rs xmlns="http://www.tei-c.org/ns/1.0" key='d1_AytR_002'>d1_AytR_002</rs>">
I tried many ways to save xml file on my filesystem, but it doesn't work. I don't know what can I do...
I only want to read an xml file, then modify the value of a node, and then save this file... But nothing happened. In PHP I don't know what the problem. In Java okay... but I need to do in PHP.
XML file:
<?xml version="1.0"?>
<node>
<pass>test</pass>
</node>
public static function saveToXML()
{
$xml = simplexml_load_file(dirname(__FILE__).'/../../../../sms_data.xml');
$xml->pass = "000";
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
echo $dom->saveXML();
}
The $xml is contains the modified values. It is good, but the save function doesn't wanna work!
I tried this too: echo $dom->save('text.xml'); But nothing. It doesn't create the text.xml file... I don't know... I have been searching for the solutions on google for one day. But I don't know what the hell is going to always wrong. I didnt get nothing. The server log is empty about it...
I am so sorry to ask this. But I don't understand this code why doesn't work... why doesn't save the modification on filesystem level!
Check if the directory exists and is writable.
thanks to this helpful community I've been enabled to make a xsl-stylesheet extracting some metainformation from xml-files on my site. Of course, I do not want to code the stylesheet directly in the xml-files, which shall be left untouched. Also, I do not want to preprocess the files in OxyGen and upload the metainfo-files.
So I simply tried this, in metainfo.php:
<?php echo '<?xml-stylesheet type="text/xsl" href="metainfo.xsl"?>'; include ('sample.xml') ?>
Still, loading metainfo.php will display the whole xml file. The source code looks fine, but when I copy it, save it as xml and open it in OxyGen, there is this little bugger '' in the code, which apperntly is called a BOM:
<?xml-stylesheet type="text/xsl" href="metainfo.xsl"?> <?xml-stylesheet type="text/xsl" href="metainfo.xsl"?>
Might this cause the trouble in the browser too? Or is it something else, more basic?
After some extra work, there's what I figured out as a solution myself:
<?php
$signatur = $_GET['signatur'];
# LOAD XML FILE
$XML = new DOMDocument();
$XML->load( 'xml/'.$signatur.'.xml' );
# START XSLT
$xslt = new XSLTProcessor();
# IMPORT STYLESHEET 1
$XSL = new DOMDocument();
$XSL->load( 'metainfo.xsl' );
$xslt->importStylesheet( $XSL );
#PRINT
print $xslt->transformToXML( $XML );
?>
I have created a XML file using PHP's simple XML, saved the file. When opening the file in php using fopen and printing the contents. my XML looks like this: (see below)
<?xml version="1.0" encoding="UTF-8"?>
<home><orderList><delivery_cost>0.00</delivery_cost><delivery_surname>TEST</delivery_surname><delivery_postcode>1234</delivery_postcode><status>1</status></orderList></home>
I want the xml file looking all indented and on new lines for each element. Does anybody know how to do this?
Thanks
You can do this using the formatOutput property of DOMDocument.
Save your XML like this instead, presuming your XML is in a variable called $yourXML, and you want to save it to a file at $xmlFilePath:
$dom = new DOMDocument();
$dom->loadXML($yourXML);
$dom->formatOutput = true;
$formattedXML = $dom->saveXML();
$fp = fopen($xmlFilePath,'w+');
fwrite($fp, $formattedXML);
fclose($fp);
Code adapted from here.
This is called "pretty printing" and SimpleXML does not do that. If you search on Stack Overflow and elsewhere on the web you'll find custom solutions that do that.
Pretty printing is good for visulation but I don't recommend saving documents in that format.
If you're still looking for a pretty-printer, you can try SimpleDOM's asPrettyXML()
include 'SimpleDOM.php';
$home = simpledom_load_string('<?xml version="1.0" encoding="UTF-8"?>
<home><orderList><delivery_cost>0.00</delivery_cost><delivery_surname>TEST</delivery_surname><delivery_postcode>1234</delivery_postcode><status>1</status></orderList></home>');
echo $home->asPrettyXML();
echo "\n"; for new line in xml
ob_start(); echo '
' . "\n";?>
I'm using PHP5 to create XML files. I have code like this:
$doc = new DOMDocument();
...
$xml_content = $doc->saveXML();
The problem is that created XML code starts with a root node like this one:
<?xml version="1.0"?>
But I want it to be like this:
<?xml version="1.0" standalone="yes" ?>
I guess I need to call some function on $doc, but I can't figure out which one?
You want to set
$doc->xmlStandalone = true;
It's not a function of the class, it's a property so it's a little harder to find in the docs. You can read about it here.