I'm using the Simple XML Framework for parsing XML files.
From a server i receive a XML-File what looks like this:
<Objects>
<Object type="A">
<name></name>
<color></color>
</Object>
<Object type="B">
<shape></shape>
<weight></weight>
</Object>
<Objects>
I have an interface (or superclass) Object and two subclasses A and B
Is it possible to de-serialize this XML-Document?
I saw in the Tutorial that there is a possibility to differentiate between subclasses with an class-attribute, but unfortunately this is not possible for me. Is there a way to chose that the framework chooses the right sub-class on the base of the type attribute?
I can't use another Framework (like JAXB) because i use Android..
Simple XML can do that too if you are willing to make one minor change to your XML. So in your example, you could change that xml to look like this:
<Objects>
<A>
<name></name>
<color></color>
</A>
<B>
<shape></shape>
<weight></weight>
</B>
<Objects>
And then you could have the following java:
#ElementListUnion({
#ElementList(entry = "A", inline = true, type = A.class),
#ElementList(entry = "B", inline = true, type = B.class)
}
private List<BaseClass> objects;
And that is all that there really is to it. Though I do not think that it can be done based on an Attribute. I could be wrong though, you might want to read the docs for that one.
This'll be tricky to do if you can't use the class attribute, like so:
<Objects>
<Object class="ObjectA">
<name></name>
<color></color>
</Object>
<Object class="ObjectB">
<shape></shape>
<weight></weight>
</Object>
<Objects>
Why replace your type="A" and type="B" attributes with classes, using a regular expression, before feeding it to the Simple XML deserializer? For instance:
xml = xml.replaceAll("type=\"([A-Za-z0-9_]+)\", "class=\"$1\"");
I was able to use this trick parsing XML generated by .NET's XML serializer, which uses the attribute xsi:type to indicate the specific class type in polymorphic lists such as this one.
Related
I am having some trouble using xpath to extract the "Payload" values below using apache-camel. I use the below xpath in my route for both of the example xml, the first example xml returns SomeElement and SomeOtherElement as expected, but the second xml seems unable to parse the xml at all.
xpath("//Payload/*")
This example xml parses just fine.
<Message>
<Payload>
<SomeElement />
<SomeOtherElement />
</Payload>
</Message>
This example xml does not parse.
<Message xmlns="http://www.fake.com/Message/1">
<Payload>
<SomeElement />
<SomeOtherElement />
</Payload>
</Message>
I found a similar question about xml and xpath, but it deals with C# and is not a camel solution.
Any idea how to solve this using apache-camel?
Your 2nd example xml, specifies a default namespace: xmlns="http://www.fake.com/Message/1" and so your xpath expression will not match, as it specifies no namespace.
See http://camel.apache.org/xpath.html#XPath-Namespaces on how to specify a namespace.
You would need something like
Namespaces ns = new Namespaces("fk", "http://www.fake.com/Message/1");
xpath("//fk:Payload/*", ns)
I'm not familiar with Apache-Camel, this was just a result of some quick googling.
An alternative maybe to just change your xPath to something like
xpath("//*[local-name()='Payload']/*)
Good luck.
I have an XML with attributes and elements/tags.
I want to know whether using an attribute or a tag is good according to performance.
Could you please give an example to compare if the content has a child tag and also if the content has a attribute.
My question is, is it possible to compare 2 attributes with same name in 2 different XML files and also here we will have huge data.
So, I want to be sure how the performance is, if i consider it as a attribute or tag.
<A Name="HRMS">
<B BName="IN">
<C Code="0001">
<IN irec="200" />
<OUT orec="230" Number="" Outname=""/>
</C>
<C Code="0004">
<IN irec="209" />
<OUT orec="209" Number="" Outname=""/>
</C>
<C Code="0008">
<IN irec="250" />
<OUT orec="250" Number="" Outname=""/>
</C>
</B>
</A>
Here, i have to compare irec with orec for a particular B name and C code
It's possible. You need a java lib like jsoup to help parse xml by path expression like jquery css selection expression.
Jsoup is a HTML parser, but html is a kind of xml application, so you can use it to parse xml content.
jsoup example:
String xml = "<root><person name=\"Bob\"><age>20</age></person></root>";
Document root = Jsoup.parse(xml);
System.out.println(root.body().html());//origin XML content
Elements persons = root.getElementsByTag("person");
Element person = persons.first();
System.out.println("The attribute 'name' of Person:" + person.attr("name"));
System.out.println(persons.select("person[name=Bob]").first().text());
You can implement compare difference function using jsoup simplily.
I have a XSL/XML parser to produce jsp/html code.
Using MVC model I need to accees spring library in order to perform i18n translation.
Thus, given the xml
<a>
...
<country>EN</country>
...
</a>
and using <spring:message code="table_country_code.EN"/> tag, choose based on the browser language, the transalation into England, Inglaterra, etc...
However, the XSL do not support <spring:message> tag.
The idea is to have a XSLT with something like this
<spring:message code="table_country_code.><xsl:value-of select="country"/>"/>`
to have the final code <spring:message code="table_country_code.EN"/> and be recognized in the final JSP/HTML based on i18n translation.
I also tried to create the spring tag in Java when I make a parse to create the XML but I sill have the same error.
The prefix "spring" for element "spring:message" is not bound.
[EDIT]
I saw some questions here, like using bean:spring but still have the same problem.
any pointers?
XSLT has to be namespace well formed XML, so you need to declare the namespace and you can not use < in attribute values.
Spring 3 - Accessing messages.properties in jsp
suggests the namespace should be
http://www.springframework.org/tags
so presumably you want an XSLT code of
<spring:message
xmlns:spring="http://www.springframework.org/tags"
code="table_country_code.{country}"
/>
where {} is an attribute value template that evaluates the XPath country
I am trying to generate xml using doxygen from java sourcecode. Doxygen doesn't parse tags like
<code>,<value> and \s\p.... correctly. It generates xml with incorrect values.
For example:
<code>0x0</code> tag is converted into <computeroutput>0x0</computeroutput>.
<para>
<computeroutput>This is code tag</computeroutput>
<value2>test value4</value2> </meta> </meta> <gid>000001</gid> <read>1</read>
</parameter> </component> </algebra>
</para>
similarly for other tags like <value> and \s\p also.
I am wondering why it happens?????
Please let me know what are all other tags also will produce the same output
and how to resolve it.
"correctly" is a bit of a misnomer when referring to xml, unless it weren't structured correctly, but I think you're referring to the tags.
If you don't like the output from doxygen why not write an xslt to make it whatever you want? I'm sure there are many doxygen.xml --> myflavor.xml transforms out there that you could use as a starting point.
About Apache XmlBeans. I use AnyType in scheme definition (xsd:anyType) as element in complex type.
Example:
<request xmlns="">
<xml>
<input1>
<string>str</string>
</input1>
</xml>
</request>
in java code
final ProcessRequest processRequest = requestDocument.addNewRequest();
XmlObject xml = processRequest.addNewXml();
xml.changeType(operationType.type);
xml.set(operationType);
and i want to see
<xml xsd:type="*opeation1NSPrefix*:*operation1Type*>
...
</xml>
but i see only <xml/>. What i doing wrong?
stupid mistake: operation1 define as anonymous type <element><complexType>..</></> in wsdl. when i define "<element type=".."/><complexType name=".."/> the problem disappeared