2012-02-15 15 views
10

Sto utilizzando la proprietà JAXB_FRAGMENT per il marshalling per il marshalling a livello di WorkSet. Il problema è che quando eseguo il marshalling attribuisco l'elemento WorkSet all'attributo xmlns ogni volta. C'è un modo per effettuare il marshalling in modo che non colleghi l'attributo xmlns? Ecco come si presenta il mio XML.JAXB Fragment Marshal senza spazio dei nomi

<Import> 
     <WorkSets> 
      <WorkSet xmlns="http://www.namespace.com"> 
       <Work> 
       <Work> 
       ... 
       .. 
       ... 
      </WorkSet> 
      <WorkSet xmlns="http://www.namespace.com"> 
       <Work> 
       <Work> 
       ... 
      </WorkSet> 
     </WorkSets> 
    </Import> 

Ecco il codice che sto utilizzando il creare quanto sopra:

FileOutputStream fos = new FileOutputStream("import.xml"); 
XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(fos); 

JAXBContext jc = JAXBContext.newInstance(WorkSet.class); 
Marshaller m = jc.createMarshaler(); 
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); 

writer.writeStartDocument(); 
writer.writeStartElement("Import"); 
writer.writeAttribute("xmlns","http://www.namespace.com"); 
writer.writeStartElement("WorkSets"); 

while(hasWorkSet){ 
m.marshal(workSet, writer) 
} 
writer.writeEndDocument(); 
writer.close(); 
+0

Volete avere la dichiarazione dello spazio dei nomi più in alto nel documento, o non vuoi affatto dichiarare un namespace? –

+0

@BlaiseDoughan Sto scrivendo manualmente i tag Importa e Workset attraverso un XMLStreamWriter, quindi ho impostato manualmente anche i loro attributi. Sto usando JAXB per eseguire il marshalling di una raccolta di istanze WorkSet, che funziona bene, ma non voglio che l'attributo xmlns sia allegato a ciascuno. Non ha inserito gli attributi xmlns su WorkSet quando ho usato JAXB per eseguire il marshalling dell'intera istanza XML insieme (Importa, Workset, Workset, Lavoro ...). È iniziato solo quando ho attivato JAXB_FRAGMENT per eseguire il marshalling di WorkSet. – TyC

+0

@BlaiseDoughan Ho cercato attraverso l'API JAXB e ancora non riesco a vedere cosa sta causando lo spazio dei nomi da mettere sull'elemento WorkSet. – TyC

risposta

8

Supponendo che si desidera che il namespace di default per il documento di essere http://www.namespace.com, si potrebbe procedere come segue:

Demo

ilI metodie XMLStreamWriter.writeNamespace(String, String) verranno utilizzati per impostare e scrivere lo spazio dei nomi predefinito per il documento XML.

package forum9297872; 

import javax.xml.bind.*; 
import javax.xml.stream.*; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(System.out); 
     writer.setDefaultNamespace("http://www.namespace.com"); 

     JAXBContext jc = JAXBContext.newInstance(WorkSet.class); 
     Marshaller m = jc.createMarshaller(); 
     m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); 

     writer.writeStartDocument(); 
     writer.writeStartElement("http://www.namespace.com", "Import"); 
     writer.writeNamespace("", "http://www.namespace.com"); 
     writer.writeStartElement("WorkSets"); 

     m.marshal(new WorkSet(), writer); 
     m.marshal(new WorkSet(), writer); 

     writer.writeEndDocument(); 
     writer.close(); 
    } 

} 

WorkSet

La mia ipotesi è che hai specificato informazioni dello spazio dei nomi nel modello JAXB.

package forum9297872; 

import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement(name="WorkSet", namespace="http://www.namespace.com") 
public class WorkSet { 

} 

uscita

Di seguito si riporta l'output eseguire il codice demo:

<?xml version="1.0" ?><Import xmlns="http://www.namespace.com"><WorkSets><WorkSet></WorkSet><WorkSet></WorkSet></WorkSets></Import> 
+0

Questo ha funzionato, ma non ho idea del perché. Non c'era un nome specificato nella classe WorkSet. Ecco il tag XmlRootElement allegato alla classe. @XmlRootElement (name = "WorkSet"). – TyC

+0

Esiste una classe 'package-info' nello stesso pacchetto della classe' WorkSet'? –

+0

Sì. Contiene quanto segue: @ javax.xml.bind.annotation.XmlSchema (namespace = "http://www.itron.com/nVanta", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) – TyC

1

Ci sono tre soluzioni per questo.

1) Creare oggetti annotati JAXB per il contenitore dei workset. Aggiungi i workset a quell'oggetto e poi fai il marshall dell'intera cosa.

2) Seguire il primo esempio in 101 ways to marshal objects with JAXB e utilizzare DocumentBuilderFactory con spazio dei nomi consapevoli.

3) Supponendo che l'oggetto jaxb si trovi in ​​un pacchetto che non dovrebbe mai avere spazi dei nomi qualificati è possibile aggiungere quanto segue all'annotazione del pacchetto: (nota: è passato un po 'di tempo dall'ultima volta che l'ho fatto e non ho provato questo codice)

@XmlSchema(namespace = "", elementFormDefault = XmlNsForm.UNQUALIFIED) 
package example; 
0

Solo un altro esempio:

try { 
     JAXBContext customerInformationRequestContext = JAXBContext.newInstance(CustomerInformationRequest.class); 
     Unmarshaller unmarshaller = customerInformationRequestContext.createUnmarshaller(); 
     StringReader stringReader = new StringReader(requestPayload); 
     XMLInputFactory xif = XMLInputFactory.newFactory(); 
     XMLStreamReader xsr = xif.createXMLStreamReader(stringReader); 
     XMLStreamReaderWrapper reader = new XMLStreamReaderWrapper(xsr); 
     CustomerInformationRequest customerInformationRequest = (CustomerInformationRequest) unmarshaller.unmarshal(reader); 
     CustomerInformationResponse customerInformationResponse = customerLookup(customerInformationRequest, sessionTransaction); 
     JAXBContext customerInformationResponseContext = JAXBContext.newInstance(CustomerInformationResponse.class); 
     Marshaller marshaller = customerInformationResponseContext.createMarshaller(); 
     StringWriter stringWriter = new StringWriter(); 
     XMLOutputFactory xof = XMLOutputFactory.newFactory(); 
     XMLStreamWriter xsw = xof.createXMLStreamWriter(stringWriter); 
     xsw = new XMLStreamWriterWrapper(xsw); 
     marshaller.marshal(customerInformationResponse, xsw); 
     String responsePayload = stringWriter.toString(); 
     return responsePayload; 
    } catch (Exception e) { 
     log.debug("The payload could not be unmarshalled.", e); 
     return null; 
    } 
Problemi correlati