2013-01-16 8 views
7

Ho usato JAXB per analizzare un XML.Come ottenere un particolare elemento (ad es. Un nodo figlio) tramite l'analisi xml JAXB senza analizzare quell'elemento come nodo.Come ottenere un elemento particolare tramite l'analisi xml di JAXB?

<?xml version="1.0" encoding="UTF-8"?> 
     <Header> 
      <From> 
      <Credential 
         domain="NetworkId"><Identity>ANXXXNNN</Identity> 
      </Credential> 
      </From> 

      <To> 
      <Credential 
         domain="NetworkId"><Identity>ANNNXXXXXT</Identity> 
      </Credential> 
      </To> 

      <To> 
      <Credential 
         domain="NetworkId"><Identity>BNNXXXT</Identity> 
      </Credential> 
      </To> 
     </Header> 

ho fatto unmarshalling come questo, funziona prestazioni fine.For, non voglio elementi come node.Is ci anyother modo per farlo?

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
dbf.setNamespaceAware(true); 
DocumentBuilder db = dbf.newDocumentBuilder(); 
    Document doc; 
    doc = db.parse(file); 
    NodeList node = (NodeList)doc.getElementsByTagName("TO"); 

    JAXBElement<ToType> element = jaxbUnmarshaller.unmarshal(node.item(0),ToType.class); 

modello oggetto è come

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "ToType", propOrder = { 
    "credential" 
}) 
public class ToType { 

    @XmlElement(name = "Credential", required = true) 
    protected CredentialType credential; 


    public CredentialType getCredential() { 
     return credential; 
    } 

    public void setCredential(CredentialType value) { 
     this.credential = value; 
    } 

}

+0

non si utilizza jaxbMarshaller ovunque nel snippet di codice. –

+0

Mi sto prendendo in giro mentre smantellamento solo – thilo

+0

Sì, c'è un altro modo di fare senza usare il nodo? – thilo

risposta

7

È possibile utilizzare un parser StAX (JSR-173) (incluso in JDK/JRE che inizia con Java SE 6). Quindi è possibile avanzare il XMLStreamReader al nodo figlio e unmarshal da lì.

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

public class Demo { 

    public static void main(String[] args) throws Exception { 
     XMLInputFactory xif = XMLInputFactory.newFactory(); 
     StreamSource xml = new StreamSource("src/forum14358769/input.xml"); 
     XMLStreamReader xsr = xif.createXMLStreamReader(xml); 

     // Advance to the "To" element. 
     while(xsr.hasNext()) { 
      if(xsr.isStartElement() && "To".equals(xsr.getLocalName())) { 
       break; 
      } 
      xsr.next(); 
     } 

     // Unmarshal from the XMLStreamReader that has been advanced 
     JAXBContext jc = JAXBContext.newInstance(ToType.class); 
     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     ToType toType = unmarshaller.unmarshal(xsr, ToType.class).getValue(); 
    } 

} 

Per ulteriori informazioni

0

si dovrà fare seguente codice per fare JAXB Unmarshalling:

unmarshaler.unmarshal(source); 

Ciò restituirà la istanza di classe che avresti generato usando il comando xjc.

Quindi è possibile accedere a qualsiasi proprietà utilizzando i metodi bean.

+0

Grazie, ho fatto come Header header = (Header) jaxbUnmarshaller.unmarshal (file). Ma sto ricevendo solo la lista degli oggetti. Se voglio ottenere l'elemento figlio (cioè "From") ho bisogno di fare così Header.From.getCredential() ... Non lo desidero. Devo accedere direttamente all'elemento "Da", non all'intestazione. Per favore, fammi chiarire. – thilo

Problemi correlati