commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Aaron Gadberry <aa...@gadberry.com>
Subject Re: XInclude Support
Date Tue, 17 May 2016 14:34:36 GMT
Hi,

To answer my own question, the document builder I was using was not
namespace or xinclude aware.  Acquiring a document builder manually and
setting a custom document builder on the XMLConfiguration allowed XInclude
to function as intended.

Here is the updated and functional code and output.

package test;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.configuration2.ConfigurationUtils;
import org.apache.commons.configuration2.XMLConfiguration;
import
org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;

public class TestConfiguration {
public static void main(String[] args) throws ConfigurationException {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setXIncludeAware(true);
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Parameters params = new Parameters();
FileBasedConfigurationBuilder<XMLConfiguration> builder = new
FileBasedConfigurationBuilder<XMLConfiguration>(
XMLConfiguration.class);
builder.configure(params.xml()
.setFile(new File("configs/test_external.xml"))
.setDocumentBuilder(db));
XMLConfiguration config = builder.getConfiguration();
System.out.println(ConfigurationUtils.toString(config));
} catch (ParserConfigurationException e) {
e.printStackTrace();
}

}
}


New Output:

[@xmlns:xi]=http://www.w3.org/2001/XInclude
ExternalElements.InternalRoot[@xml:base]=test_internal.xml
ExternalElements.InternalRoot.InternalElement[@name]=[1, 2, 3]


Thanks!


On Tue, May 17, 2016 at 8:06 AM, Aaron Gadberry <aaron@gadberry.com> wrote:

> Hi,  My question is specifically on commons-configuration2.
>
> I cannot seem to use XInclude within an XMLConfiguration file.  Perhaps I
> am doing something incorrectly, or perhaps it is unsupported, but I can't
> seem to find documentation either way.
>
> This is my example program, files and output.
>
> TestConfiguration.java
>
> package test;
>
> import java.io.File;
>
> import org.apache.commons.configuration2.ConfigurationUtils;
> import org.apache.commons.configuration2.XMLConfiguration;
> import
> org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
> import org.apache.commons.configuration2.builder.fluent.Parameters;
> import org.apache.commons.configuration2.ex.ConfigurationException;
>
> public class TestConfiguration {
>     public static void main(String[] args) throws ConfigurationException {
>         Parameters params = new Parameters();
>         FileBasedConfigurationBuilder<XMLConfiguration> builder = new
> FileBasedConfigurationBuilder<XMLConfiguration>(
>                 XMLConfiguration.class);
>         builder.configure(params.fileBased().setFile(
>                 new File("configs/test_external.xml")));
>         XMLConfiguration config = builder.getConfiguration();
>         System.out.println(ConfigurationUtils.toString(config));
>     }
> }
>
>
> configs/test_external.xml
>
> <?xml version="1.0" encoding="UTF-8"?>
> <ExternalRoot>
>     <ExternalElements>
>         <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
>             href="configs/test_internal.xml" />
>     </ExternalElements>
> </ExternalRoot>
>
>
> configs/test_internal.xml
>
> <?xml version="1.0" encoding="UTF-8"?>
> <InternalRoot>
>     <InternalElement name="1" />
>     <InternalElement name="2" />
>     <InternalElement name="3" />
> </InternalRoot>
>
>
> Result:
>
> ExternalElements.xi:include[@xmlns:xi]=http://www.w3.org/2001/XInclude
> ExternalElements.xi:include[@href]=configs/test_internal.xml
>
>
> Expected Result:
>
> ExternalElements.InternalRoot.InternalElement[@name]=1,2,3
>
>
> Any help is appreciated!
>
> Thanks,
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message