Page 1 of 1
create xml with inline DTD
Posted: Sun Apr 26, 2009 7:23 pm
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
Re: create xml with inline DTD
Posted: Sun Apr 26, 2009 7:55 pm
by McInfo
What have you tried, and why hasn't it worked?
Edit: This post was recovered from search engine cache.
Re: create xml with inline DTD
Posted: Mon Apr 27, 2009 6:05 am
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
Re: create xml with inline DTD
Posted: Mon Apr 27, 2009 9:41 am
by McInfo
May I see the contents of catalog.dtd?
Edit: This post was recovered from search engine cache.
Re: create xml with inline DTD
Posted: Mon Apr 27, 2009 11:13 am
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)>
]>
Re: create xml with inline DTD
Posted: Mon Apr 27, 2009 2:20 pm
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.
Re: create xml with inline DTD
Posted: Tue Apr 28, 2009 6:00 am
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
Re: create xml with inline DTD
Posted: Tue Apr 28, 2009 10:56 am
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.
Re: create xml with inline DTD
Posted: Tue Apr 28, 2009 5:51 pm
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.
Re: create xml with inline DTD
Posted: Tue Apr 28, 2009 6:45 pm
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.