Reputation: 457
One of my client to integrate provide an XML with attribute name "_1", "_2" ... etc. e.g.
<element _1="attr1" _2="attr2">
using JAXB to generate the class, the getter method of the attribute will be get1() and get2()
However in my JSP pages, using JSTL and EL, sure I cannot access the value through
${variable.1}
How can I access the value using EL correctly?
Upvotes: 1
Views: 153
Reputation: 149037
You could use an external binding file to rename the property generate by JAXB:
schema.xsd
Below is a sample XML schema based on your post:
<?xml version="1.0" encoding="UTF-8"?>
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org"
xmlns:tns="http://www.example.org"
elementFormDefault="qualified">
<element name="element1">
<complexType>
<attribute name="_1" type="string" />
<attribute name="_2" type="string" />
</complexType>
</element>
</schema>
binding.xml
An external binding file is used to customize how Java classes are generated from the XML schema. Below we'll use an external binding file to rename the generated properties.
<jaxb:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jaxb:bindings schemaLocation="schema.xsd">
<jaxb:bindings node="//xsd:attribute[@name='_1']">
<jaxb:property name="one"/>
</jaxb:bindings>
<jaxb:bindings node="//xsd:attribute[@name='_2']">
<jaxb:property name="two"/>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
XJC Call
Below is an example of how you reference the binding file when using the XJC tool.
xjc -b binding.xml schema.xsd
Element1
Below is what the generated class will look like:
package forum12259754;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "element1")
public class Element1 {
@XmlAttribute(name = "_1")
protected String one;
@XmlAttribute(name = "_2")
protected String two;
public String getOne() {
return one;
}
public void setOne(String value) {
this.one = value;
}
public String getTwo() {
return two;
}
public void setTwo(String value) {
this.two = value;
}
}
Upvotes: 1