I am trying to write a function that updates the Site Map of a website or technically adds an entry to sitemap.xml file. The standard structure of a Google sitemap can be seen here: http://www.sitemappro.com/google-sitemap.html
Following is the code of that function:
function AddEntry($loc,$lastmod,$changefreq,$priority){
$xmlDoc = new DOMDocument();
$xmlDoc->load("sitemap.xml");
$url []=array('loc' => $loc, 'lastmod' => $lastmod, 'changefreq'=> $changefreq, 'priority' =>$priority );
$r=$xmlDoc->createElement("url");
$xmlDoc->appendChild($r);
foreach($url as $key=>$value)
{
$r->appendChild($xmlDoc->createElement($key))->appendChild($xmlDoc->createTextNode($value));
}
$xmlDoc->save();
}
The above code is not working and giving this error:
"Fatal error: Uncaught exception 'DOMException' with message 'Invalid Character Error' in..."
Can you please help by correcting my code? Thanks in advance.
As pointed out in a comment the issue should be in this line
$r->appendChild($xmlDoc->createElement($key))->appendChild($xmlDoc->createTextNode($value));
Most likely you're adding some url characters to your XML. The problem is that you must escape them or surround your $value with a CDATA section. Otherwise the generated XML will be invalid and the xml library will throw that kind of exceptions.
P.S. I don't remember if the htmlentities function is good also for xml, I suppose that, but you'll need to investigate about that.
Related
I used the following code to parse the HTML of another site but it display the fatal error:
$html=file_get_html('http://www.google.co.in');
Fatal error: Call to undefined function file_get_html()
are you sure you have downloaded and included php simple html dom parser ?
You are calling class does not belong to php.
Download simple_html_dom class here and use the methods included as you like it. It is really great especially when you are working with Emails-newsletter:
include_once('simple_html_dom.php');
$html = file_get_html('http://www.google.co.in');
As everyone have told you, you are seeing this error because you obviously didn't downloaded and included simple_html_dom class after you just copy pasted that third party code,
Now you have two options, option one is what all other developers have provided in their answers along with mine,
However my friend,
Option two is to not use that third party php class at all! and use the php developer's default class to perform same task, and that class is always loaded with php, so there is also efficiency in using this method along with originality plus security!
Instead of file_get_html which not a function defined by php developers use-
$doc = new DOMDocument();
$doc->loadHTMLFile("filename.html");
echo $doc->saveHTML(); that's indeed defined by them. Check it on php.net/manual(Original php manual by its devs)
This puts the HTML into a DOM object which can be parsed by individual tags, attributes, etc.. Here is an example of getting all the 'href' attributes and corresponding node values out of the 'a' tag. Very cool....
$tags = $doc->getElementsByTagName('a');
foreach ($tags as $tag) {
echo $tag->getAttribute('href').' | '.$tag->nodeValue."\n";
}
P.S. : PLEASE UPVOTE IF YOU LIKED MY ANSWER WILL HELP MY REPUTATION ON STACKOVERFLOW, THIS PEOPLES THINK I'M NOOB!
It looks like you're looking for simplexml_load_file which will load a file and put it into a SimpleXML object.
Of course, if it is not well-formatted that might cause problems. Your other option is DomObject::loadHTMLFile. That is a good deal more forgiving of badly formed documents.
If you don't care about the XML and just want the data, you can use file_get_contents.
$html = file_get_contents('http://www.google.co.in');
to get the html content of the page
in simple words
download the simple_html_dom.php from here Click here
now write these line to your Php file
include_once('simple_html_dom.php');
and start your coading after that
$html = file_get_html('http://www.google.co.in');
no error will be displayed
Try file_get_contents.
http://www.php.net/manual/en/function.file-get-contents.php
I am using a very cool php library(whatever it is called) called SimplePie. I am using the latest version.
I have this code:
$url = 'http://www.seobook.com/feeds.shtml';
$SimplePieFeed->set_feed_url($url);
$SimplePieFeed->force_feed(true);
$SimplePieFeed->enable_order_by_date(true);
$success = $SimplePieFeed->init();
if( !$SimplePieFeed->error() ) {
foreach( $SimplePieFeed->get_items() as $item ) {
......
}
} else {
print_r( $SimplePieFeed->error() );
}
Why is it that when I run this code I'm getting this kind of error:
This XML document is invalid, likely due to invalid characters. XML error: not well-formed (invalid token) at line 8, column 76
I try to run this one on Simplepie's demo and everything is going well. Why is it that when I run it on my end i'm having that kind of error? Is it because of a cache? I noticed that Simplepie is storing feeds in a cache. I have tried $SimplePieFeed->enable_cache(false); but still i'm getting that error. I'm not even sure if that's related to that kind of error. LOL!
Your help would be greatly appreciated and rewarded! :Thank you very much!
Simple there is problem in your xml file you should remake the .xml file,if you are using wordpress simple use plugin called google site maps its pretty good.
If you using some thing else like php, or html base site you should make valid xml document maker like some listed here,
xml-sitemaps.com
xmlgrid.net (editor viewer)
web-site-map.com
May it help you little in your case.
all
I create a soap server use below code. I found it is a little hard to create wsdl document in PHP .so I decide use non-wsdl mode.
$soapServer = new SoapServer(NULL,array('uri'=>'http://com.test.env',
'encoding'=>'UTF-8'));
$soapServer->addFunction("workprocess_orders_api_add");
$soapServer->addFunction("workprocess_list_api_get_local_name");
$soapServer->handle();
but when I try to access this program, it print nothing. both web-browser and program(I use SoapClient)
$client = new SoapClient(NULL,array('uri'=>'http://com.test.env',
'location'=>'http://localhost/~breeze.kay/ams/workprocess/api/soap/',
'trace'=>1,
'style' => SOAP_DOCUMENT,
'use' => SOAP_LITERAL));
var_dump($client->__call('workprocess_orders_api_add',array())); //print null
var_dump($client->__getFunctions()); //print null
I tired call some function like this at client-side:
echo $client->sayHello('test');
I get error:
Fatal error: Uncaught SoapFault exception: [Client] looks like we got no XML document in /Users/breeze.kay/Sites/ad-test/soap.php:15
where is wrong ? I have no idea.:( the code looks ok. why there is no print?
OK, I tried it out.
the problem is the request url must append ?wsdl ,like this: http://localhost/wbs/api.php?wsdl
but I use url-rewrite for my sites. the rewrite rule break the original url struct.
by the way, dose somebody know where talk about the url rule for soap request?
I read some manual not find it.
The XML document not found error usually occurs when there is an empty space before php opening or after closing tag, try clearing that and test.
Recently I get an exception while I try to implement a special template engine.
Mine problem with DOMDocument load is in fact that I use some place holders in href and it will be replaced after with real values. I want to store that template and to use it each time when I need it, here is code sample
$this->dom = new DOMDocument;
$load_html = $this->dom->loadHTML($html);
And nesty part of HTML looks like
Visit our web site
and exception is
Fatal error: DOMDocument::loadXML() [<a href='domdocument.loadxml'>domdocument.loadxml</a>]: EntityRef: expecting ';' in Entity
I was try to find a some way to skip that check but couldn't find anything.
This causes no error in PHP 5.3. You must have some other HTML that is causing this. Normally you see this when you use an entity with no ; after it.
Foo   Bar
That throws that same error for me. Look for some entity without a ; on it. Browsers will render that, but it is not correct.
I found what was the problem. In my URL it was & sign, I was replace it with & and now it works.
I'm building a web form in which administrators on my site can add XML to a textarea and submit it to be stored in a database table, but I'm a little confused as to the best method of parsing the XML.
The PHP script needs to parse the XML and if there are any parse errors it should return the error message and line/column where the parser stopped to the administrator who submitted the form. After parsing it, it needs to access the DOM to run several checks for the existance of nodes and attributes using XPath.
If I use xml_create_parser() and xml_parse(), I can get the detailed error information that I'm after if false is returned. However, I can't access the DOM of the XML after I parse it. If I use DOMDocument::loadXML(), from what I've read, it doesn't throw exceptions for parse errors, it just outputs them to the PHP log.
Would it be a great performance hit if I first tried xml_parse() and then if that's successful, run DOMDocument::loadXML(), considering the files are mostly smaller than 10KB with a few being 10-20KB? Does anyone know a better way?
You can enable libxml_use_internal_errors and then -if DOMDocument::load() failed- query the detailed error messages with libxml_get_errors()
<?php
$xml = '<a>
<b>xyz</b>
<c>
</a>';
libxml_use_internal_errors(true);
$doc = new DOMDocument;
if ( !$doc->loadxml($xml) ) {
$errors = libxml_get_errors();
var_dump($errors);
}