Given XML like this:
...
<Sport SportId="1">
<Name language="en">Soccer</Name>
<Name language="fi">Jalkapallo</Name>
...
</Sport>
...
How can I, using the Simple XML Framework , read the two values into fields in a Java class? (The <Sport>
element is already correctly mapped to the corresponding class.)
public class Sport {
...
String nameEn;
String nameFi;
...
}
I've tried approaches like:
@Element(name = "Name")
@Path("Name[@language='en']")
String nameEn;
But the parsing fails with:
Exception in thread "main" org.simpleframework.xml.core.PathException:
Invalid index for path '[@language='en']' in field 'nameEn'
Also, omitting @Element
like this:
@Path("Name[@language='en']")
String nameEn;
...parsing doesn't crash, but nameEn
value stays null.
I'd like the matching to be based on the language attribute (instead of ordering), but I'm wondering if that's possible (maybe XPath support in Simple Framework is limited?).
Have you tried getting the text of the element explicitly? ie Name[@language='en']/text()
Your Xpath is selecting the element , not the text of the element, which can cause some XML engines to choke.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.