2013-04-05 4 views
5

Attualmente sto utilizzando JAXB per analizzare i file xml. Ho generato le classi necessarie attraverso un file xsd. Tuttavia, i file xml che ricevo non contengono tutti i nodi dichiarati nelle classi generate. Quanto segue è un esempio di struttura del mio file xml:Gestione dei nodi mancanti con JAXB

<root> 
<firstChild>12/12/2012</firstChild> 
<secondChild> 
<firstGrandChild> 
<Id> 
    </name> 
    <characteristics>Description</characteristics> 
    <code>12345</code> 
</Id> 
</firstGrandChild> 
</secondChild> 
</root> 

mi confronto con le seguenti due casi:

  1. Il nodo <name> è presente nelle classi generate ma non nei file XML
  2. Il nodo non ha valore

In entrambi i casi, il valore è impostato su null. Mi piacerebbe essere in grado di differenziare quando il nodo è assente dal file XML e quando è presente ma ha un valore nullo. Nonostante le mie ricerche, non ho trovato un modo per farlo. Ogni aiuto è più che benvenuto

Grazie mille in anticipo per il vostro tempo e aiutare

saluti

+0

xsi: nil è tuo amico! Leggi tutto qui http://stackoverflow.com/questions/774192/what-is-the-correct-way-to-represent-null-xml-elements~~V~~3rd – MariuszS

+0

Grazie per la risposta! Come ho capito, questo è per impostare il valore del nodo su "" invece di null. Il problema che sto affrontando è che per JAXB, un nodo vuoto e un nodo inesistente nel file xml vengono gestiti allo stesso modo. Quindi non posso sapere se ho ricevuto il nodo nel file o l'ho fatto e il suo valore era nullo – user2249868

+0

Il seguente esempio aiuterà con 'xsi: nil': http://blog.bdoughan.com/2012/04/binding -to-json-xml-handling-null.html –

risposta

3

Un JAXB (JSR-222) implementazione non chiamerà il metodo set per i nodi assenti. È possibile inserire la logica nel metodo set per tracciare se è stato chiamato o meno.

public class Foo { 

    private String bar; 
    private boolean barSet = false; 

    public String getBar() { 
     return bar; 
    } 

    public void setBar(String bar) { 
     this.bar = bar; 
     this.barSet = true; 
    } 

} 

UPDATE

JAXB tratterà anche nodi vuoti come avente un valore di vuoto String.

Java Modello

import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement 
public class Root { 

    private String foo; 
    private String bar; 

    public String getFoo() { 
     return foo; 
    } 

    public void setFoo(String foo) { 
     this.foo = foo; 
    } 

    public String getBar() { 
     return bar; 
    } 

    public void setBar(String bar) { 
     this.bar = bar; 
    } 

} 

Demo

import java.io.File; 
import javax.xml.bind.*; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(Root.class); 

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     File xml = new File("src/forum15839276/input.xml"); 
     Root root = (Root) unmarshaller.unmarshal(xml); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(root, System.out); 
    } 

} 

input.xml/Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<root> 
    <foo></foo> 
</root> 
+0

Ho bisogno di gestire lo stesso per BigInteger Tipo di dati e raccolte, ma per BigInteger il flag booleano è sempre falso –