create xml with inline DTD

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pnikkos
Forum Newbie
Posts: 5
Joined: Sun Apr 26, 2009 7:21 pm

create xml with inline DTD

Post by pnikkos »

Hi,

i am trying to create with php an xml file, but i want to write the dtd at the beginning.

I can write xml files, but i can find a way to write the dtd inside the xml file.

Is there a way to that?

Thanks in advance
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: create xml with inline DTD

Post by McInfo »

What have you tried, and why hasn't it worked?

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 1:01 pm, edited 1 time in total.
pnikkos
Forum Newbie
Posts: 5
Joined: Sun Apr 26, 2009 7:21 pm

Re: create xml with inline DTD

Post by pnikkos »

so far i am doing this:

Code: Select all

$imp = new DOMImplementation;
 
// Creates a DOMDocumentType instance
    $dtd = $imp->createDocumentType('catalog', '', 'catalog.dtd');
    
    
    // Creates a DOMDocument instance
    $doc = $imp->createDocument("", "", $dtd);
 
    // Set other properties
    $doc->encoding = 'UTF-8';
    $doc->standalone = false;
    
  $r = $doc->createElement( "catalog" );
  $doc->appendChild( $r );
  
  foreach( $friends as $user )
  {
  $b = $doc->createElement( "user" );
  
  $name = $doc->createElement( "name" );
  $name->appendChild(
  $doc->createTextNode( $user['name'] )
  );
  $b->appendChild( $name );
  
  $surname = $doc->createElement( "surname" );
  $surname->appendChild(
  $doc->createTextNode( $user['surname'] )
  );
  $b->appendChild( $surname );
  
  $email = $doc->createElement( "email" );
  $email->appendChild(
  $doc->createTextNode( $user['email'] )
  );
  $b->appendChild( $email );
  
  $r->appendChild( $b );
  }
 
  $doc->save("lista.xml");
  
 
  $xml_doc = new DOMDocument();
  $xml_doc->Load("lista.xml");
  if(!$doc->validate())
        echo "<p>Validation failes<p>";
  
  viewXML("lista.xml");


This code failed to validate the xml file, because it has not the dtd.

So, i want to put the dtd in the xml file and pass the validation.


Is that feasible?


Thanks again
Last edited by Benjamin on Mon Apr 27, 2009 8:10 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: create xml with inline DTD

Post by McInfo »

May I see the contents of catalog.dtd?

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 1:02 pm, edited 1 time in total.
pnikkos
Forum Newbie
Posts: 5
Joined: Sun Apr 26, 2009 7:21 pm

Re: create xml with inline DTD

Post by pnikkos »

Here it is:

Code: Select all

<!DOCTYPE catalog [
  <!ELEMENT catalog (user+)>
  <!ELEMENT user (name,surname,email)>
  <!ELEMENT name      (#PCDATA)>
  <!ELEMENT surname   (#PCDATA)>
  <!ELEMENT email (#PCDATA)>
]>
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: create xml with inline DTD

Post by McInfo »

New catalog.dtd

Code: Select all

<!ELEMENT catalog (user+)>
    <!ELEMENT user (name,surname,email)>
        <!ELEMENT name (#PCDATA)>
        <!ELEMENT surname (#PCDATA)>
        <!ELEMENT email (#PCDATA)>
Where is viewXML() defined?

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 1:02 pm, edited 1 time in total.
pnikkos
Forum Newbie
Posts: 5
Joined: Sun Apr 26, 2009 7:21 pm

Re: create xml with inline DTD

Post by pnikkos »

viewxml is working fine. But before that i want to validate the xml.

So i want to put the dtd declaration inside the xml. Is there a way to do that?


Thanks again for your interest
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: create xml with inline DTD

Post by McInfo »

Your XML is validated on line 47. If the error message does not appear, the XML has passed validation.

Code: Select all

if(!$doc->validate())
    echo "<p>Validation failes<p>";
In the generated lista.xml, the DTD is referenced on line 2.

Code: Select all

<!DOCTYPE catalog SYSTEM "catalog.dtd">
The way you have your script set up, you need to define the doctype in a separate file (catalog.dtd). If you want to define the doctype inside lista.xml, I think you need to use something other than DOMImplementation.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 1:03 pm, edited 1 time in total.
pnikkos
Forum Newbie
Posts: 5
Joined: Sun Apr 26, 2009 7:21 pm

Re: create xml with inline DTD

Post by pnikkos »

The error message is appearing, so the xml doesn't pass the validation.

I tried an external file catalog.dtd, but it didn't worked, so i am trying to find a solution to put the dtd inside xml.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: create xml with inline DTD

Post by McInfo »

This is the code I have been using to debug your issue. Everything works. Maybe there is something wrong with your data source?

document-1.php

Code: Select all

<?php
error_reporting(E_ALL);
 
$imp = new DOMImplementation;
 
// Creates a DOMDocumentType instance
$dtd = $imp->createDocumentType('catalog', '', 'catalog2.dtd');
 
// Creates a DOMDocument instance
$doc = $imp->createDocument("", "", $dtd);
 
// Set other properties
$doc->encoding = 'UTF-8';
$doc->standalone = false;
 
$r = $doc->createElement( "catalog" );
$doc->appendChild( $r );
 
$friends = array
(   0 => array
    (   'name' => 'Bob'
    ,   'surname' => 'Smith'
    ,   'email' => 'bobsmith@example.com'
    )
,   1 => array
    (   'name' => 'Joe'
    ,   'surname' => 'Brown'
    ,   'email' => 'joebrown@example.com'
    )
);
 
foreach( $friends as $user )
{
    $b = $doc->createElement( "user" );
   
    $name = $doc->createElement( "name" );
    $name->appendChild(
        $doc->createTextNode( $user['name'] )
    );
    $b->appendChild( $name );
   
    $surname = $doc->createElement( "surname" );
    $surname->appendChild(
        $doc->createTextNode( $user['surname'] )
    );
    $b->appendChild( $surname );
   
    $email = $doc->createElement( "email" );
    $email->appendChild(
        $doc->createTextNode( $user['email'] )
    );
    $b->appendChild( $email );
   
    $r->appendChild( $b );
}
 
$doc->save("lista.xml");
 
$xml_doc = new DOMDocument();
$xml_doc->Load("lista.xml");
if(!$doc->validate())
    echo "<p>Validation failes<p>";
 
//viewXML("lista.xml");
if (!headers_sent())
    header('Content-Type: text/xml');
if (is_file('lista.xml'))
    echo file_get_contents('lista.xml');
?>
catalog2.dtd

Code: Select all

<!ELEMENT catalog (user+)>
    <!ELEMENT user (name,surname,email)>
        <!ELEMENT name (#PCDATA)>
        <!ELEMENT surname (#PCDATA)>
        <!ELEMENT email (#PCDATA)>
Edit: This post was recovered from search engine cache.
Post Reply