I'm currently trying to get xmlns value. I need to validate xml files. I'm using Jackson-dataformat-xml to deserialize xml files to objects. So far I've not found a way to get that value.
#JacksonXmlProperty(isAttribute = true) does not seem to be working for xmlns.
xml
<Document xmlns="urn:...">
...
</Document>
Java
#Data
public class Document {
#JacksonXmlProperty(isAttribute = true)
private String xmlns;
}
As I understand xmlns isn't exposed as a attribute. Is there any I could get the value somehow with Jackson?
xmlns value is namespace declarations. it's not a attributes but metadata, so parsers do not expose them as attributes.
Related
I am trying to define XML from Java object binding using JAXB. Everything works fine except i want to have currency tag with xml tag like this
<money currency="INR">1230</money>
I am not able to find the solution for this how can i accomadate **currency="INR" ** in the same tag
#XmlElement(name="money")
private Integer money;
Saw that #xmlElement doesnt has any properties for the same
I've faced issue to combine JAXB and Jackson annotation together:
public class Document {
String someField;
#JsonIgnore
#XmlElementWrapper(name = "someWrapper")
#XmlElement(name = "someElement")
List<String> someCollection;
}
I need to be able to marshall and unmarshall field 'someCollection' to xml, but to have the opportunity to serialize 'Document' object to json without such field.
But this field appears in final json string
So, if I've understood right - Jackson sees both JsSON and XML annotations. So I can not force to serialize something and do not serialize in the same moment.
It is not possible, I think
I have a problem when I am getting the attributes of a xml file. These attributes are written like this:
#XmlAttribute(name="xml:lang")
With #XmlAttribute I get attributes that they are written on a one word. If I leave this annotation #XmlAttribute(name="xml:lang") I get null
How can I get attribute's value of "xml:lang"?
Thanks.
xml is clearly a namespace prefix. You can try stating the namespace it with:
#XmlAttribute(name = "lang",
namespace = javax.xml.XMLConstants.XML_NS_URI)
I'm using CXF to generate java classes from a WSDL/XSD and later back to XML (for JMS).
In one of the generated classes, it says:
#XmlElement(namespace = "http://www.example.com/", nillable = true)
protected Datum datum;
All good, but when I put a null value:
test.setDatum(null);
I get the following XML generated:
<datum xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
The xsi:nil="true" is what i want, but I don't want the namespace xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance", because this namespace is already specified at the root element. Also the validation fails at the test platform we have to use.
How do I get rid of the extra namespace in the element?
I did it the other way around. I wrote a interceptor that checks for certain attributes. If found, I add the xsi:nil="true" attribute to the xml.
How can I covert a an xml file to a simple java bean?
Its a simple xml file without any xsd, which was generated from a java bean, which I don't have access to.
I tried using xmlbeans to first generate the xmd from xml and then to generate classes from the xsd. I got a bunch of classes. I am looking for a single java bean class.
JAXB
JAXB (JSR-222) provides an easy way to convert objects to XML. There are many open source implementations of this standard including:
Metro JAXB (the reference implementation included in Java SE 6)
EclipseLink JAXB (MOXy), I'm the tech lead
Apache JaxMe
JAXB has a default mapping for Java objects to XML. This mapping can be customized through the application of annotations.
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.Element;
#XmlRootElement
public class Address {
private String street;
private String city;
private String state;
private String country;
#XmlElement(name="postal-code")
private String postalCode;
}
Would correspond to the following XML:
<address>
<street>123 A Street</street>
<city>Any Town</city>
<state>A State</state>
<postal-code>12345</postal-code>
</address>
EclipseLink JAXB (MOXy)
MOXy has an XPath based mapping extension. This means we can take our same Address class and map it to Google's geocode format:
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.eclipse.persistence.oxm.annotations.XmlPath;
#XmlRootElement(name="kml")
#XmlType(propOrder={"country", "state", "city", "street", "postalCode"})
public class Address {
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:Thoroughfare/ns:ThoroughfareName/text()")
private String street;
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:LocalityName/text()")
private String city;
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:AdministrativeAreaName/text()")
private String state;
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:CountryNameCode/text()")
private String country;
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:PostalCode/ns:PostalCodeNumber/text()")
private String postalCode;
}
The above class corresponds to the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0" xmlns:ns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
<Response>
<Placemark>
<ns:AddressDetails>
<ns:Country>
<ns:CountryNameCode>US</ns:CountryNameCode>
<ns:AdministrativeArea>
<ns:AdministrativeAreaName>CA</ns:AdministrativeAreaName>
<ns:SubAdministrativeArea>
<ns:Locality>
<ns:LocalityName>Mountain View</ns:LocalityName>
<ns:Thoroughfare>
<ns:ThoroughfareName>1600 Amphitheatre Pkwy</ns:ThoroughfareName>
</ns:Thoroughfare>
<ns:PostalCode>
<ns:PostalCodeNumber>94043</ns:PostalCodeNumber>
</ns:PostalCode>
</ns:Locality>
</ns:SubAdministrativeArea>
</ns:AdministrativeArea>
</ns:Country>
</ns:AddressDetails>
</Placemark>
</Response>
</kml>
For more Information
XPath Based Mapping - Geocode Example
Map to Element based on an Attribute Value with EclipseLink JAXB (MOXy)
XPath Based Mapping
Try Castor Mapping.
You could use a tool like Castor or JAXB to map the XML to a java class. Castor is fairly easy to use.