create xml with inline DTD
Moderator: General Moderators
create xml with inline DTD
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
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
What have you tried, and why hasn't it worked?
Edit: This post was recovered from search engine cache.
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.
Re: create xml with inline DTD
so far i am doing this:
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
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.
Reason: Changed code type from text to php.
Re: create xml with inline DTD
May I see the contents of catalog.dtd?
Edit: This post was recovered from search engine cache.
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.
Re: create xml with inline DTD
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
New catalog.dtd
Where is viewXML() defined?
Edit: This post was recovered from search engine cache.
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.
Last edited by McInfo on Tue Jun 15, 2010 1:02 pm, edited 1 time in total.
Re: create xml with inline DTD
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
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
Your XML is validated on line 47. If the error message does not appear, the XML has passed validation.
In the generated lista.xml, the DTD is referenced on line 2.
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.
Code: Select all
if(!$doc->validate())
echo "<p>Validation failes<p>";Code: Select all
<!DOCTYPE catalog SYSTEM "catalog.dtd">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.
Re: create xml with inline DTD
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.
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
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
catalog2.dtd
Edit: This post was recovered from search engine cache.
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');
?>Code: Select all
<!ELEMENT catalog (user+)>
<!ELEMENT user (name,surname,email)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT surname (#PCDATA)>
<!ELEMENT email (#PCDATA)>