How to get a particular element through JAXB xml parsing?

天大地大妈咪最大 提交于 2019-12-05 23:38:13

问题


I have used JAXB to parse an XML.How to get a particular element(ie a child node) through JAXB xml parsing without parsing that element as node.

<?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>

I have done unmarshalling like this,It works fine.For performance,I dont want the elements as node.Is there anyother way to do?

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);

Object model is like

@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;
    }

}


回答1:


A StAX (JSR-173) parser (included in the JDK/JRE starting with Java SE 6) can be used. Then you can advance the XMLStreamReader to the child node and unmarshal from there.

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();
    }

}

For More Information

  • http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html



回答2:


You will have to do following code for doing JAXB Unmarshalling:

unmarshaler.unmarshal(source);

This will return you the instance of class which you would have generated using xjc command.

Then you can access any property using bean methods.



来源:https://stackoverflow.com/questions/14358769/how-to-get-a-particular-element-through-jaxb-xml-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!