We have an existing document tree. We want to wrap some of the elements inside this tree up within a element.
Depending on where in the tree we are, the element will hold very different content.
So I have a DocumentPromptLanguage class and a DocumentRouterLanguage class. They have different parents and different children but it makes sense that in XML they are both called <language>.
Is this possible without adapters or must the XML representation disambiguate by element name?
Sample:
<?xml version="1.0" encoding="utf-8"?>
<doc>
<info>
<language>
<iso639>en</iso639>
<value>This is a sample document</value>
</language>
<language>
<iso639>es</iso639>
<value>Se trata de un documento de muestra</value>
</language>
</info>
<someElement>
<route>
<language>
<iso639>en</iso639>
<possibleValues>Yes|No|Maybe</possibleValues>
<prefix>For</prefix>
</language>
<language>
<iso639>es</iso639>
<possibleValues>sí|not|tal vez</possibleValues>
<prefix>para</prefix>
</language>
<when>Tuesday</when>
<afterTime>17.30</afterTime>
<goto></goto>
</route>
</someElement>
</doc>
Yes the class mapped to the route element can have a property mapped with #XmlElement(name="language"), and so can the class mapped to the info element. This because the mappings are scoped by class.
Related
It is easy to parse XML in which tags name are fixed. In XStream, we can simply use #XStreamAlias("tagname") annotation. But how to parse XML in which tag name is not fixed. Suppose I have following XML :
<result>
<result1>
<fixed1> ... </fixed1>
<fixed2> ... </fixed2>
</result1>
<result2>
<item>
<America>
<name> America </name>
<language> English </language>
</America>
</item>
<item>
<Spain>
<name> Spain </name>
<language> Spanish </language>
</Spain>
</item>
</result2>
</result>
Tag names America and Spain are not fixed and sometimes I may get other tag names like Germany, India, etc.
How to define pojo for tag result2 in such case? Is there a way to tell XStream to accept anything as alias name if tag name is not known before-hand?
if it is ok for you to get the tag from inside the tag itself (field 'name'), using Xpath, you can do:
//result2/*/name/text()
another option could be to use the whole element, like:
//result2/*
or also:
//result2/*/name()
Some technologies (specifically, data binding approaches) are optimized for handling XML whose structure is known at compile time. Others (like DOM and other DOM-like tree models - JDOM, XOM etc) are designed for handling XML whose structure is not known in advance. Use the tool for the job.
XSLT and XQuery try to blend both. In their schema-aware form, they can take advantage of static structure information when it is available. But more usually they are run in "untyped" mode, where there is no a-priori knowledge of element names or structure, and everything is handled as it comes. The XSLT rule-based processing paradigm is particularly well suited to "semi-structured" XML whose content is unpredictable or variable.
i need create next structure en xml
<ser:myobjet soapenv: encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<xml xsi:type='xsd.string'>
<!^[CDATA[<!xml version="1.0" encoding="iso-8859-1"?>
<object>
<service>
<request>
<idservice>65445</idservice>
<date>14-08-2016</date>
<additionalData>
<user>user</user>
<city>mycity</city>
</additionalData>
</request>
</service>
</object>]]>
</xml>
See I have a father and his children, yet one of them has their own children
<request>
<idservice>65445</idservice>
<date>14-08-2016</date>
<additionalData>
<user>user</user>
<city>mycity</city>
</additionalData>
</request>
I'm really lost and do not know how to create that structure.
Someone knows or some example of how to create it?
As I return a CDATA me and I need access to those tags, any idea how to do it?
Thank you.
I'm just curious about default namespaces. I have the follow xml:
<root xmlns="myNamespace">
<someElement xmlns="anotherNamespace">
<data>Info</data>
</someElement>
<anotherElement>
<moreData>Info2</moreData>
</anotherElement>
</root>
My guess is that myNamespace is inherit for <root> and <anotherElement>. And anotherNamespace is a default namespace in <someElement> so in this element and for its child's overrides the other myNamespace.
I know that I can rewrite the above xml code like:
<my:root xmlns:my="myNamespace">
<a:someElement xmlns:a="anotherNamespace">
<a:data>Info</a:data>
</a:someElement>
<my:anotherElement>
<my:moreData>Info2</my:moreData>
</my:anotherElement>
</my:root>
I think that both are totally valid but I have some problems with some xml beans implementation in java which doesn't accept the first one, so I'm curious if there is an xml specification where specifies if first aproach is or not correct.
The first approach is valid. In xml-names specification in section 6.2 Namespace Defaulting it explains:
The scope of a default namespace declaration extends from the
beginning of the start-tag in which it appears to the end of the
corresponding end-tag, excluding the scope of any inner default
namespace declarations.
And also includes this example:
<!-- initially, the default namespace is "books" -->
<book xmlns='urn:loc.gov:books'
xmlns:isbn='urn:ISBN:0-395-36341-6'>
<title>Cheaper by the Dozen</title>
<isbn:number>1568491379</isbn:number>
<notes>
<!-- make HTML the default namespace for some commentary -->
<p xmlns='http://www.w3.org/1999/xhtml'>
This is a <i>funny</i> book!
</p>
</notes>
</book>
I have this XML:
<Body xmlns:wsu="http://mynamespace">
<Ticket xmlns="http://othernamespace">
<Customer xlmns="">Robert</Customer>
<Products xmlns="">
<Product>a product</>
</Products>
</Ticket>
<Delivered xmlns="" />
<Payment xlmns="">cash</Payment>
</Body>
I am using Java to read it as a DOM document. I want remove the empty namespace attributes (i.e., xmlns=""). Is there any way to do that?
You need to understand that xmlns is a very special attribute. Basically, the xmlns="" is so that your Customer element is in the "unnamed" namespace, rather than the http://othernamespace namespace (and likewise for other elements which would otherwise inherit a default namespace from their ancestors).
If you want to get rid of the xmlns="", you basically need to put the elements into the appropriate namespace - so it's changing the element name. I don't think the W3C API lets you change the name of an element - you may well need to create a new element with the appropriate namespaced-name, and copy the content. Or if you're responsible for creating the document to start with, just use the right namespace.
According to this, you can make use of xs:key and xs:keyref when marshalling and unmarshalling data in JAXB 2.x.
However, I can't find a working example of this being done anywhere.
What we're doing is setting a lookup section in each XML message containing the details for the reference/code values (id, name, description, etc), and then have the data elements later in the message refer back to these items using their key. XML schema defines and supports this through xs:keyref and xs:key (xs:IDREF is not an allowed option).
What I'd like to do is have my JAXB unmarshaller follow these refs dynamically, replacing the key with the referenced object.
Could anybody refer me to an example of this being done?
Are you talking about a compound key situtation?
<directory>
<employee>
<eID>123</eID>
<country>CA</country>
</employee>
<employee>
<eID>123</eID>
<country>US</country>
</employee>
<employee>
<eID>456</eID>
<country>US</country>
</employee>
<phone-number>
<contact eID="123" country="US"/>
</phone-number>
</directory>
If so EclipseLink JAXB (MOXy) could be used:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA/CompoundPrimaryKeys