geoffjay
geoffjay

Reputation: 413

Saving XML files with external entities using libxml without merging into one

My application loads configuration from XML and works with multiple files read in as entities, but when I save the document back out it combines all files into one. This isn't the worst thing but it would be better if the changes ended up in the separate entities. I'm willing to use XSD syntax instead of DTD if that's an option, what I'd like to avoid if possible though is having to load each file as separate documents and combine them manually.

Sample config:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE cfg SYSTEM "cfg.dtd" [
  <!ENTITY sec SYSTEM "sec.xml">
]>
<cfg>
  <!-- stuff -->

  <!-- load external section -->
  &sec;

  <!-- more stuff -->
</cfg>

and trivial entity file sec.xml fwiw:

<?xml version="1.0" encoding="ISO-8859-1"?>
<sec>
  <prop name="myprop">0</prop>
</sec>

I haven't included the DTD because I don't think that matters.

Using Vala I load the document with

doc = Xml.Parser.read_file (file_name, null,
                            Xml.ParserOption.DTDATTR |
                            Xml.ParserOption.NOENT |
                            Xml.ParserOption.DTDVALID)

and save with

doc->save_file (file_name);

Maybe there's an intermediary step using a writer that I'm not seeing.

Upvotes: 0

Views: 687

Answers (1)

nwellnhof
nwellnhof

Reputation: 33658

The first step is to remove the NOENT option, disabling the expansion of &sec;. Without NOENT you have to handle entity nodes manually. But with NOENT, the entity is replaced during parsing which isn't reversible. Maybe it helps in your case to parse the document twice, once with and once without NOENT?

Upvotes: 1

Related Questions