I had problems to use a DTD from a file. It needed to be resolved relatively and it contained characters that made DomDocument failed to resolve the file.
Encoding and an absolute filename did not help much. So I used the data:// streamwrapper ( http://php.net/manual/en/wrappers.data.php ) as a work-around:
<?php
// relative or absolute filename
$path = '...';
// convert file contents into a filename
$data = file_get_contents($path);
$systemId = 'data://text/plain;base64,'.base64_encode($data);
// ...
// Creates a DOMDocumentType instance
$dtd = $aImp->createDocumentType('qualified name', '', $systemId);
?>
Works like a charm.
DOMImplementation::createDocumentType
(PHP 5)
DOMImplementation::createDocumentType — Boş bir DOMDocumentType nesnesi oluşturur
Açıklama
DOMDocumentType DOMImplementation::createDocumentType
([ string
$belgeAdı = NULL
[, string $publicId = NULL
[, string $systemId = NULL
]]] )Boş bir DOMDocumentType nesnesi oluşturur. Öğe bildirimleri ve gösterimler kullanılabilir değildir. Öğe gönderimi dönüşümleri ve öntanımlı öznitelik eklemeleri yapılmaz.
Değiştirgeler
-
belgeAdı -
Oluşturulacak belgenin nitelikli adı.
-
publicId -
Harici alt kümenin genel betimleyicisi.
-
systemId -
Harici alt kümenin sistem betimleyicisi.
Dönen Değerler
ownerDocument özelliği NULL olmak üzere yeni bir DOMDocumentType nesnesi döner.
Örnekler
Örnek 1 - Bir DTD ekleyerek bir belge oluşturmak
<?php
// Yeni bir DOMImplementation nesnesi oluşturalım
$imp = new DOMImplementation;
// Bir DOMDocumentType nesnesi oluşturalım
$dtd = $imp->createDocumentType('graph', '', 'graph.dtd');
// Bir DOMDocument nesnesi oluşturalım
$dom = $imp->createDocument("", "", $dtd);
// Diğer özellikleri tanımlayalım
$dom->encoding = 'UTF-8';
$dom->standalone = false;
// Boş bir eleman oluşturalım
$element = $dom->createElement('graph');
// Bir eleman ekleyelim
$dom->appendChild($element);
// Belgeyi çıktılayalım
echo $dom->saveXML();
?>
Yukarıdaki örneğin çıktısı:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE graph SYSTEM "graph.dtd"> <graph/>
Hatalar/İstisnalar
-
DOM_NAMESPACE_ERR -
belgeAdına göre saptanan isim alanı ile ilgili bir hata varsa bu hata oluşur.
Ayrıca Bakınız
- DOMImplementation::createDocument() - Belge elemanı belirtilen DOMDocument nesnesini oluşturur
until-all-bytes-are-free at example dot org
09-Apr-2011 04:28
