问题描述
我需要以不同的方式配置应用程序.配置使用XML.不同的配置通常具有相同的部分.例如,两种配置之间的差异就是电子邮件地址的差异.
I need to configure an application in different ways.The configuration is in XML.Different configurations will mostly have common parts. For example the difference between two configurations will be a difference of an email address.
因此,与其重复我想过的通用部分,没有办法是拥有一个基本的XML文件并仅覆盖/添加其中的一部分并生成结果XML文件.
So rather than duplicating the common parts I was thinking is there a way to have a base XML file and override/add to only parts of it and generate a resultant XML file.
我已经在Maven中看到了此功能-您有一个父pom,而子pom继承自它.最终使用的是有效pom.
I've seen this functionality in Maven - you have a parent pom and the child pom inherits from it. What is used in the end is the effective pom.
1)是否已经有这样做的库(用于任何编程语言,但最好是用于Java的库)?
1) Are there already libraries ( for any programming language but preferably for Java ) that does this?
2)如果不是实现此目的的好方法?我对XML技术(例如XSD和XSLT)不是很熟悉.因此,显而易见的方式对我而言可能并不那么明显.
2) If not what would be a good approach to implement this?I'm not very familiar with XML technologies such as XSD and XSLT. So the obvious way might not be as obvious to me.
我们非常感谢您的帮助.
Any help is much appreciated.
推荐答案
我可能会使用 XInclude 一个>. Java中似乎有一些支持,因此我将为您的特定堆栈搜索带有限定符的"xinclude for java".
I would probably use XInclude. There seems to be some support in Java, so I would search for "xinclude for java" with a qualifier for your specific stack.
使用xerces可以很好地应对;默认为关闭状态,因此您必须打开功能 对于XInclude的常规阅读,本文显示了该概念在工作中. For a general read on XInclude, this article shows the concept at work. 这篇关于生成从其他XML文件继承的XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
try {
dbf.setFeature("http://apache.org/xml/features/xinclude", true);
}
catch (ParserConfigurationException e) {
System.err.println("could not set parser feature");
}