xpath从根下获取xml文件,转换成实体对象



import java.io.StringReader;


import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;


import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;


public class xPathToObj {
public static Object xmlToObject(Class c,String xml,Object... objs){
Object obj = null;
JAXBContext context = null;
Unmarshaller unmarshaller = null;
try{
context = JAXBContext.newInstance(c);
    unmarshaller = context.createUnmarshaller();
    
       DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
       domFactory.setNamespaceAware(false);//不解析命名空间
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xml)));
//Document document = builder.parse("xml/CaregAppRoot.xml");
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expression = xpath.compile("//Root");
NodeList result = (NodeList) expression.evaluate(document,
XPathConstants.NODESET);
        
        obj = unmarshaller.unmarshal(result.item(0));
        
        
}catch(JAXBException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}  
return obj;
}

private static String getXml() {
String xml="aaa";
return xml;
}
public static void main(String[] args) {
CaRegAppRoot t=(CaRegAppRoot)xmlToObject(CaRegAppRoot.class,getXml());
System.out.println(t.getCaRegAppList().get(0).getCustomername());
}

}




@XmlRootElement(name="Root")
@XmlAccessorType(XmlAccessType.FIELD)
public class CaRegAppRoot {
@XmlElement(name="RegApp")
private List caRegAppList;


public List getCaRegAppList() {
return caRegAppList;
}


public void setCaRegAppList(List caRegAppList) {
this.caRegAppList = caRegAppList;
}

}


@XmlAccessorType(XmlAccessType.FIELD)
public class CaRegApp {
    /**
     * 用户ID
     */
@XmlTransient
    private String userid;


    /**
     * 客户名称
     */
@XmlElement(name="CustomerName")
    private String customername;


    /**
     * 证书受理ID
     */
@XmlElement(name="CertID")
    private String certid;


    /**
     * 证书序列号
     */
@XmlElement(name="SerialNumber")
    private String serialnumber;


    /**
     * 应用方的客户编号
     */
@XmlElement(name="ExtendInfo")
    private ExtendInfo extendinfo;


    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    /**
     * 应用有效期限
     */
    @XmlElement(name="EndDate")
    @XmlJavaTypeAdapter(XmlDateAdapter.class)
    private Date enddate;


    /**
     * 证书应用服务状态
     */
    @XmlElement(name="Status")
    private String status;


    /**
     * 应用类型
     */
    @XmlElement(name="IssumType")
    private String issumtype;


    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    /**
     * 创建时间
     */
    @XmlTransient
    private Date cjsj;


    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    /**
     * 最后修改时间
     */
    @XmlTransient
    private Date zhxgsj;


    public String getUserid() {
        return userid;
    }


    public void setUserid(String userid) {
        this.userid = userid == null ? null : userid.trim();
    }


    public String getCustomername() {
        return customername;
    }


    public void setCustomername(String customername) {
        this.customername = customername == null ? null : customername.trim();
    }


    public String getCertid() {
        return certid;
    }


    public void setCertid(String certid) {
        this.certid = certid == null ? null : certid.trim();
    }


    public String getSerialnumber() {
        return serialnumber;
    }


    public void setSerialnumber(String serialnumber) {
        this.serialnumber = serialnumber == null ? null : serialnumber.trim();
    }


    public ExtendInfo getExtendinfo() {
return extendinfo;
}


public void setExtendinfo(ExtendInfo extendinfo) {
this.extendinfo = extendinfo;
}


public Date getEnddate() {
        return enddate;
    }


    public void setEnddate(Date enddate) {
        this.enddate = enddate;
    }


    public String getStatus() {
        return status;
    }


    public void setStatus(String status) {
        this.status = status == null ? null : status.trim();
    }


    public String getIssumtype() {
        return issumtype;
    }


    public void setIssumtype(String issumtype) {
        this.issumtype = issumtype == null ? null : issumtype.trim();
    }


    public Date getCjsj() {
        return cjsj;
    }


    public void setCjsj(Date cjsj) {
        this.cjsj = cjsj;
    }


    public Date getZhxgsj() {
        return zhxgsj;
    }


    public void setZhxgsj(Date zhxgsj) {
        this.zhxgsj = zhxgsj;
    }
}

你可能感兴趣的:(java,jaxb,xpath)