10

I am trying to format visually how my XML file looks when it is output. Right now if you go here and view the source you will see what the file looks like.

The PHP I have that creates the file is: (Note, $links_array is an array of urls)

        header('Content-Type: text/xml');
        $sitemap = new DOMDocument;
        
        // create root element
        $root = $sitemap->createElement("urlset");
        $sitemap->appendChild($root);
         
        $root_attr = $sitemap->createAttribute('xmlns'); 
        $root->appendChild($root_attr); 

        $root_attr_text = $sitemap->createTextNode('http://www.sitemaps.org/schemas/sitemap/0.9'); 
        $root_attr->appendChild($root_attr_text); 

        
        
        foreach($links_array as $http_url){
        
                // create child element
                $url = $sitemap->createElement("url");
                $root->appendChild($url);
                
                $loc = $sitemap->createElement("loc");
                $lastmod = $sitemap->createElement("lastmod");
                $changefreq = $sitemap->createElement("changefreq");
                
                $url->appendChild($loc);
                $url_text = $sitemap->createTextNode($http_url);
                $loc->appendChild($url_text);
                
                $url->appendChild($lastmod);
                $lastmod_text = $sitemap->createTextNode(date("Y-m-d"));
                $lastmod->appendChild($lastmod_text);
                
                $url->appendChild($changefreq);
                $changefreq_text = $sitemap->createTextNode("weekly");
                $changefreq->appendChild($changefreq_text);
                
        }
        
        $file = "sitemap.xml";
        $fh = fopen($file, 'w') or die("Can't open the sitemap file.");
        fwrite($fh, $sitemap->saveXML());
        fclose($fh);
    }

As you can tell by looking at the source, the file isn't as readable as I would like it to be. Is there any way for me to format the nodes?

Thanks,
Levi

1
  • You can add a xml header and open it in FireFox. This will show a nice formatted version off your xml document Commented Jan 6, 2010 at 10:37

2 Answers 2

10

Checkout the formatOutput setting in DOMDocument.

$sitemap->formatOutput = true
Sign up to request clarification or add additional context in comments.

Comments

0

not just PHP, there is a stylesheet for XML: XSLT, which can format XML into sth looks good.

2 Comments

Ah ok, I have done some basic XSLT stylesheets before. I was hoping there was a way to at least put line breaks in after a node was created with PHP.
answer from above formatOutput do the job? your question seems mis leading this way....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.