Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

maven - XML configuration inheritance, avoid duplications

Imagine a situation when you have about 10+ big XML configuration files for multiple environments which are almost identical.

Sometimes you have to add a new option, which leads to modification of all those 10..20..30+ files with the same data.

You make mistakes. You get conflicts when merging with other branches. You got nervous and depressed.

Are there any good tools that provide something like inheritance for plain XML files (not Spring.cgf or POM)?

Or I have to write an ant or maven bicycle-script on my own?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use entities/entity references for referencing and reusing content at multiple places, within or across files.

An XML file declaring and using an entity looks like this:

<!DOCTYPE doc [
  <!ENTITY myent "<x>Text content of myent</x>">
]>
<doc>
  &myent;
  &myent;
</doc>

This XML file is handled as if

<doc>
  <x>Text content of myent</x>
  <x>Text content of myent</x>
</doc>

had been specified. That is, the entity reference &myent; is substituted by its replacement text <x>Text content of myent</x>.

This works also with replacement text stored in an external file (called an external entity). Assuming the file above is stored as doc.xml, you can reference it from another XML file like this:

<!DOCTYPE otherdoc [
  <!ENTITY doc SYSTEM "doc.xml">
]>
<otherdoc>
  &doc;
</otherdoc>

and an XML parser will treat it as if

<otherdoc>
  <doc>
    <x>Text content of myent</x>
    <x>Text content of myent</x>
  </doc>
</otherdoc>

had been specified.

Using entities, you can organize common XML content without redundancy within or accross files. Hope that helps.

Edit: note that you have to adapt the DOCTYPE - it must match the document element of your XML


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...