commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Maurizio Lattuada <maurizio.lattu...@gmail.com>
Subject Re: Fwd: Parse XML configuration file with namespaces
Date Wed, 25 Nov 2015 08:37:45 GMT
Hi @all,


@Siegfried: all my XSD files are stored locally. In fact, when I call
the "load()" method (setSchemaValidation is true), the XML is loaded
correctly. By the way, I introduced on purpose some errors in the XML
file and it was not correctly loaded, meaning that the validation is
really active and working (so, XSD files are correctly found).

@Oliver: thanks for the hint, but I think there is a strange behavior
in my opinion.

    Iterator<String> it = xmlPersonConfiguration.getKeys();
    while (it.hasNext()) {
        LOGGER.info(it.next());
    }
    final String string = namespace + Tags.PERSON; // namespace is a
parameter equals to "p:"
    xmlPerson = xmlPersonConfiguration.configurationsAt(string);
    LOGGER.info("--> Found " + xmlPerson.size() + " persons (key: " +
string + ")");

This lead to have:
2015-11-25 09:08:10,320 [c.v.t.b.r.ConfigReader:402  ] INFO  -
parsePerson : p:person/p:title
2015-11-25 09:08:10,324 [c.v.t.b.r.ConfigReader:402  ] INFO  -
parsePerson : p:person/p:firstName
2015-11-25 09:08:10,326 [c.v.t.b.r.ConfigReader:402  ] INFO  -
parsePerson : p:person/p:lastName
...all the tags here are correctly logged...
2015-11-25 09:08:10,346 [c.v.t.b.r.ConfigReader:402  ] INFO  -
parsePerson : @xmlns:ath
2015-11-25 09:08:10,346 [c.v.t.b.r.ConfigReader:402  ] INFO  -
parsePerson : @xmlns:com
2015-11-25 09:08:10,346 [c.v.t.b.r.ConfigReader:402  ] INFO  -
parsePerson : @xmlns:p
2015-11-25 09:08:10,347 [c.v.t.b.r.ConfigReader:402  ] INFO  -
parsePerson : @xmlns:xsi
2015-11-25 09:08:10,347 [c.v.t.b.r.ConfigReader:402  ] INFO  -
parsePerson : @xsi:schemaLocation
2015-11-25 09:08:10,720 [c.v.t.b.r.ConfigReader:406  ] INFO  -
parsePerson :  --> Found 0 persons (key: p:person)

Question: since my xml file has this structure (I omit the namespace
declaration for ease of reading):
<p:persons>
  <p:person>
    <p:title>...</p:title>
    ...
  </p:person>
  <p:person>
    <p:title>...</p:title>
    ...
  </p:person>
</p:persons>

Why "p:person" is not in the keys, but only the sub-tags of p:person?
When I had my xml with the XSD, but without namespaces, looking for
"person" worked fine, so I was able to iterate on the 2 persons
available in the XML example above.

By the way, I also tried to look for "p:person/p:title", that is a key
found by calling the "getKeys()" method:
    xmlPerson = xmlPersonConfiguration.configurationsAt("p:person/p:title");
    LOGGER.info("--> Found " + xmlPerson.size() + " persons");

but the xmlPerson.size is always equal to 0.
FYI xmlPersonConfiguration is created and initialized as:

            XMLConfiguration xmlPersonConfiguration = new XMLConfiguration();
            xmlPersonConfiguration.setExpressionEngine(new
XPathExpressionEngine());
            xmlPersonConfiguration.setSchemaValidation(true);
            xmlPersonConfiguration.setFileName("...");
            xmlPersonConfiguration.load();

If I don't call the "setExpressionEngine" method and I search for tags
like "p:person.p:title" or "p:person", all these entries are correctly
found and I can iterate over them.

Any other suggestions?


thanks in advance for the feedback.

2015-11-24 21:26 GMT+01:00 Oliver Heger <oliver.heger@oliver-heger.de>:
> Hi Maurizio,
>
> in order to debug your problem, you can call the configuration's
> getKeys() method. From the iterator returned you should see the
> available keys. These keys can then also be used for querying properties.
>
> HTH
> Oliver
>
> Am 24.11.2015 um 13:54 schrieb Maurizio Lattuada:
>> Hi guys,
>>
>> I'm dealing with a strange problem while parsing an XML file (with
>> namespaces) using commons configuration 1.10.
>> First of all, let me say that when I call the "load()" method on the
>> following XML file (validation is active), it is loaded flawless.
>>
>> This is an extract of the xml to be parsed:
>> <!-- persons.xml-->
>> <?xml version="1.0" encoding="UTF-8"?>
>> <p:persons
>>     action="create"
>>     xmlns:ath="tdm:configuration:authentication"
>>     xmlns:com="tdm:configuration:common"
>>     xmlns:p="tdm:configuration:entities"
>>     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>     xsi:schemaLocation="tdm:configuration:entities ../xsd/model/persons.xsd ">
>>     <p:person>
>>         <p:title>Dr.</p:title>
>>         ...
>>     </p:person>
>>     <p:person>
>>         <p:title>Mr.</p:title>
>>         ...
>>     </p:person>
>> </p:person>
>>
>> This is an extract of the persons.xsd, as you can see there are a
>> couple of imports
>> <?xml version="1.0" encoding="UTF-8"?>
>> <xs:schema xmlns="tdm:configuration:entities"
>> xmlns:common="tdm:configuration:common"
>>     xmlns:authentication="tdm:configuration:authentication"
>> targetNamespace="tdm:configuration:entities"
>>     xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
>>     <xs:import schemaLocation="../namespace/common.xsd"
>> namespace="tdm:configuration:common" />
>>     <xs:import schemaLocation="auth_mean.xsd"
>> namespace="tdm:configuration:authentication" />
>>     ....
>> </xs:schema>
>>
>>
>> And finally an extract of the code to setup the parse and to read the document:
>> XMLConfiguration xmlPersonConfiguration = new XMLConfiguration();
>> xmlPersonConfiguration.setDelimiterParsingDisabled(true); // needed
>> because I've attribute with multiple values like "a,b,c"
>> xmlPersonConfiguration.setExpressionEngine(new XPathExpressionEngine());
>> xmlPersonConfiguration.setSchemaValidation(true);
>> xmlPersonConfiguration.setFileName("persons.xml");
>> xmlPersonConfiguration.load();
>> List<HierarchicalConfiguration> xmlPersons;
>> xmlPersons = xmlPersonConfiguration.configurationsAt("person");
>>
>> Shortly, either I try to search for (last statement):
>>  * person
>>  * p:person
>>  * //person
>>  * //p:person
>>  * /p:persons/p:person
>>  *...
>>
>> No elements are found.
>> According to the commons configuration documentation, "The XML Parser
>> will then use the schema defined in the XML document to validate it.
>> Enabling schema validation will also enable the parser's namespace
>> support.".
>> Please note: before I had such xml and xsd files without namespaces
>> and everything worked fine.
>>
>> I'm pretty sure I'm doing something wrong, but I can't figure what.
>> Any idea about this behavior?
>>
>> Thanks for your kind feedback
>>
>> --
>>  Maurizio Lattuada
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>



-- 
 Maurizio Lattuada

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message