Resteasy / JAXB; How to avoid adding a namespace to an element in an <any> tag? (List <Item> in JAXB)

I am going to simplify my classes and output as much as possible, but basically what I need is I want to add org.w3c.dom.Element (representing an atom reference in this case) to the JAXB object I'm returning. The JAXB class looks something like this:

 import javax.xml.bind.annotation.XmlAnyElement; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlType; import org.w3c.dom.Element; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "People", namespace = "main", propOrder = { "any", "persons" }) public class People { @XmlAnyElement protected List<Element> any; @XmlElement(name = "person", namespace = "main") protected List<Person> persons; [...] } 

I create an element using a template, which I create as follows:

 import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.SAXException; public class ElementGen { public Element getTemplate() throws DOMException, SAXException, ParserConfigurationException { final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); final Schema schema = sf.newSchema(new StreamSource( Thread.currentThread().getContextClassLoader().getResourceAsStream(ATOM_XSD))); final DocumentBuilderFactory docBuilder = DocumentBuilderFactory.newInstance(); docBuilder.setSchema(schema); final Document doc = docBuilder.newDocumentBuilder().newDocument(); linkTemplate = doc.createElementNS(ATOM_NAMESPACE, ATOM_LINK); return linkTemplate; } } 

(Actually, this is not what the class looks like, I just try to compile something as simple as possible in order to test it without any external mess).

Then I clone this template using linkTemplate.cloneNode(false);

Now everything works in that it returns xml, but it is strange that the xml that I get has additional namespaces:

 <atom:link xmlns:ns3="main" xmlns="" href="href" rel="rel"/> 

If I add linkTemplate.setAttribute("xmlns", null); , the namespace "xmlns: ns3" will disappear, and I will get:

 <atom:link xmlns="" href="href" rel="rel"/> 

But there seems to be no way to remove this xmlns = "". Am I creating an item incorrectly? Or maybe something else is wrong? I pretty much do not understand why this is an addition at all, so any help / explanation would be appreciated.

Edit: I believe this should be related to the namespace of the document that I use to create the element, but I'm not sure how to fix it. Is there a way to set (XML) targetNamespace in a document?

Edit 2: I'm not sure if it adds anything useful to everyone, but with a lot of experimentation, I found that linkTemplate.setAttribute("xmlns:" + anything, null); creates a link to xmlns:[anything]="" and removes any others that would otherwise be generated.

Editing 3: The corresponding xsd bits used to create JAXB objects are as follows:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <xs:schema version="1.0" xmlns="main" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:atom="http://www.w3.org/2005/Atom" targetNamespace="main" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:complexType name="People"> <xs:sequence> <xs:any namespace="##other" processContents="skip" minOccurs="0" maxOccurs="unbounded"/> <xs:element name="person" type="Person" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> [attributes] </xs:complexType> [other types etc.] 
+6
source share
4 answers

Since none of the suggestions here worked for me, I decided to go the other way. I ended up rewriting the listener. RESTEasy adds to Marshaller with its listener. This listener then calls the RESTEasy listener (if present) before manually adding links to the RESTServiceDiscovery field (you should get this field using reflection and disable access check with field.setAccessible (true) before getting the object).

0
source

- value ATOM_LINK = "link"? if so, it should be "atom: link" and remove the setPrefix () call.

0
source

I believe the problem is that the creation of the DocumentBuilderFactory that you create should be a namespace.

 public class ElementGen { public Element getTemplate() throws DOMException, SAXException, ParserConfigurationException { final DocumentBuilderFactory docBuilder = DocumentBuilderFactory.newInstance(); docBuilder.setNamespaceware(true); final Document doc = docBuilder.newDocumentBuilder().newDocument(); linkTemplate = doc.createElementNS(ATOM_NAMESPACE, ATOM_LINK); return linkTemplate; } } 

If you are directly manipulating the xmlns attribute, somethibng is incorrect.

0
source

Have you tried to set elementFormDefault to "UNqualified"?

0
source

Source: https://habr.com/ru/post/900286/


All Articles