0

I am trying to put ENTITY in externaL DTD file but its not showing values of ENTITY in XML when it is parsed.

But its working when I combine the XML and DTD in one file below is my code for XML and DTD

info.xml

<!DOCTYPE info SYSTEM "./Note.dtd">  <info>   
<info>
<company>&company_info;</company>   
<privacy_policy>&privacy_policy;</privacy_policy>  </info>
</info>

Note.dtd

<!ENTITY company_info "<name>Test Company</name><location>Berlin</location><phone>1800-000-000</phone>">
<!ENTITY privacy_policy "Lorem ipsum dolor sit amet, consectetur adipisicing elit, .">

info.xml and Note.dtd is in same folder

Anup Singh
  • 1,513
  • 3
  • 16
  • 32

2 Answers2

1

I'm not sure what you're using to parse the XML, but I know Xerces will throw errors about <info> not being declared if you use the file extension ".dtd".

Try changing "Note.dtd" to "Note.ent".

I did this with your sample files and the errors went away. Also, I was able to do an identity transform using Saxon on your original info.xml and the entity references were resolved without error.

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
  • Currently I tried to open XML directly in browser firefox & safari but I am getting error in both browsers. My main aim is to convert XML into json using JSON-LIB available at http://json-lib.sourceforge.net/ – Anup Singh Feb 02 '12 at 08:37
  • 1
    @AnupSingh - Firefox (and safari) won't load external DTDs or external entities. https://developer.mozilla.org/en/XML_in_Mozilla#DTDs_and_Other_External_Entities – Daniel Haley Feb 02 '12 at 08:47
  • ah!.. hard luck.. Thanks for response.. can you please suggest me that "what is the preferred way to convert XML to JSON that resolves external entity references". – Anup Singh Feb 02 '12 at 08:57
  • @AnupSingh - Sorry, I don't have any experience with JSON. That would be a great new question though. :-) – Daniel Haley Feb 02 '12 at 16:06
0

<p>
i had the same problem..just use php instead

<?php
libxml_disable_entity_loader(false);
// code injection from the client side
$file='<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE login SYSTEM "http://localhost/e.ent">
<login><user>&name;</user><pass>password</pass></login>';
//
$dom=new DOMDocument();
$dom->loadXML($file,LIBXML_NOENT | LIBXML_DTDLOAD);
$element=$dom->getElementsByTagName('user')[0];
echo $element->nodeValue;
?></p>