JAXB XML前缀问题

时间:2014-09-11 13:46:31

标签: jaxb pojo prefix

我有一个低于一的输入xml文件。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cxp:exportedDefect xmlns:cxp="http://export.coverity.com/v7">
    <user>admin</user>
    <project>tev</project>
    <timeStamp>2014-05-23T08:45:08.916+03:00</timeStamp>
    <cxp:mergedDefect>
        <checkerName>FORWARD_NULL</checkerName>
        <checkerSubcategory>deref_constant_null</checkerSubcategory>
        <cid>10549</cid>
        <componentName>Default.Other</componentName>
        <defectStateAttributeValues>
            <attributeDefinitionId>
                <name>DefectStatus</name>
            </attributeDefinitionId>
            <attributeValueId>
                <name>Dismissed</name>
            </attributeValueId>
        </defectStateAttributeValues>
        <defectStateAttributeValues>
            <attributeDefinitionId>
                <name>Classification</name>
            </attributeDefinitionId>
            <attributeValueId>
                <name>False Positive</name>
            </attributeValueId>
        </defectStateAttributeValues>
        <defectStateAttributeValues>
            <attributeDefinitionId>
                <name>Action</name>
            </attributeDefinitionId>
            <attributeValueId>
                <name>Undecided</name>
            </attributeValueId>
        </defectStateAttributeValues>
        <defectStateAttributeValues>
            <attributeDefinitionId>
                <name>Severity</name>
            </attributeDefinitionId>
            <attributeValueId>
                <name>Unspecified</name>
            </attributeValueId>
        </defectStateAttributeValues>
        <defectStateAttributeValues>
            <attributeDefinitionId>
                <name>Fix Target</name>
            </attributeDefinitionId>
            <attributeValueId>
                <name>Untargeted</name>
            </attributeValueId>
        </defectStateAttributeValues>
        <defectStateAttributeValues>
            <attributeDefinitionId>
                <name>Legacy</name>
            </attributeDefinitionId>
            <attributeValueId>
                <name>False</name>
            </attributeValueId>
        </defectStateAttributeValues>
        <defectStateAttributeValues>
            <attributeDefinitionId>
                <name>Owner</name>
            </attributeDefinitionId>
            <attributeValueId>
                <name>Unassigned</name>
            </attributeValueId>
        </defectStateAttributeValues>
        <defectStateAttributeValues>
            <attributeDefinitionId>
                <name>TranslatedOwner</name>
            </attributeDefinitionId>
            <attributeValueId>
                <name>Unassigned</name>
            </attributeValueId>
        </defectStateAttributeValues>
        <defectStateAttributeValues>
            <attributeDefinitionId>
                <name>OwnerName</name>
            </attributeDefinitionId>
            <attributeValueId>
                <name>etevgul</name>
            </attributeValueId>
        </defectStateAttributeValues>
        <defectStateAttributeValues>
            <attributeDefinitionId>
                <name>Comment</name>
            </attributeDefinitionId>
            <attributeValueId>
                <name></name>
            </attributeValueId>
        </defectStateAttributeValues>
        <defectStateAttributeValues>
            <attributeDefinitionId>
                <name>ExternalReference</name>
            </attributeDefinitionId>
            <attributeValueId>
                <name></name>
            </attributeValueId>
        </defectStateAttributeValues>
        <domain>STATIC_JAVA</domain>
        <filePathname>/user/user-service-impl/src/main/java/com/ericsson/enk/imcp/core/user/service/impl/UserServiceImpl.java</filePathname>
        <firstDetected>2014-04-07T13:47:03.730+03:00</firstDetected>
        <firstDetectedSnapshotId>10014</firstDetectedSnapshotId>
        <functionDisplayName>com.ericsson.enk.imcp.core.user.service.impl.UserServiceImpl.authenticate(com.ericsson.enk.imcp.core.common.domain.Email, java.lang.String)</functionDisplayName>
        <lastDetected>2014-05-12T17:06:44.339+03:00</lastDetected>
        <lastDetectedSnapshotId>10020</lastDetectedSnapshotId>
        <lastFixed>2014-05-09T17:15:27.543+03:00</lastFixed>
        <lastTriaged>2014-04-24T16:21:17.912+03:00</lastTriaged>
        <mergeKey>e4a7c34660b74135e992f11cdfc5b16d</mergeKey>
        <occurrenceCount>1</occurrenceCount>
    </cxp:mergedDefect>
    <latestSnapshotId>10020</latestSnapshotId>
    <streamDefects>
        <cxp:streamDefect>
            <checkerSubcategoryId>
                <checkerName>FORWARD_NULL</checkerName>
                <domain>STATIC_JAVA</domain>
                <subcategory>deref_constant_null</subcategory>
            </checkerSubcategoryId>
            <cid>10549</cid>
            <defectStateAttributeValues>
                <attributeDefinitionId>
                    <name>DefectStatus</name>
                </attributeDefinitionId>
                <attributeValueId>
                    <name>Dismissed</name>
                </attributeValueId>
            </defectStateAttributeValues>
            <defectStateAttributeValues>
                <attributeDefinitionId>
                    <name>Classification</name>
                </attributeDefinitionId>
                <attributeValueId>
                    <name>False Positive</name>
                </attributeValueId>
            </defectStateAttributeValues>
            <defectStateAttributeValues>
                <attributeDefinitionId>
                    <name>Action</name>
                </attributeDefinitionId>
                <attributeValueId>
                    <name>Undecided</name>
                </attributeValueId>
            </defectStateAttributeValues>
            <defectStateAttributeValues>
                <attributeDefinitionId>
                    <name>Fix Target</name>
                </attributeDefinitionId>
                <attributeValueId>
                    <name>Untargeted</name>
                </attributeValueId>
            </defectStateAttributeValues>
            <defectStateAttributeValues>
                <attributeDefinitionId>
                    <name>Severity</name>
                </attributeDefinitionId>
                <attributeValueId>
                    <name>Unspecified</name>
                </attributeValueId>
            </defectStateAttributeValues>
            <defectStateAttributeValues>
                <attributeDefinitionId>
                    <name>Legacy</name>
                </attributeDefinitionId>
                <attributeValueId>
                    <name>False</name>
                </attributeValueId>
            </defectStateAttributeValues>
            <defectStateAttributeValues>
                <attributeDefinitionId>
                    <name>Comment</name>
                </attributeDefinitionId>
                <attributeValueId>
                    <name></name>
                </attributeValueId>
            </defectStateAttributeValues>
            <id>
                <defectTriageId>10345</defectTriageId>
                <defectTriageVerNum>1</defectTriageVerNum>
                <id>21194</id>
                <verNum>2</verNum>
            </id>
            <streamId>
                <name>IMCP_V3-java</name>
            </streamId>
        </cxp:streamDefect>
    </streamDefects>
    <cxp:checkerProperties>
        <subcategoryShortDescription>Explicit null dereferenced</subcategoryShortDescription>
    </cxp:checkerProperties>
</cxp:exportedDefect>

正如您所看到的,其中一些包含命名空间frefix,而其中一些不包含。我试图将xml模型化为pojo而不是默认。我无法设置映射内部前缀对象。

不知怎的,我想到jaxb正在丢弃内部前缀。

我的根xml元素是:

package com.ericsson.integration.jira.jaxb.model;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import java.io.Serializable;
import java.util.*;

/**
 * Created by olgunkaya on 11.09.2014.
 */
@XmlRootElement (name = "exportedDefect", namespace = "http://export.coverity.com/v7")

public class CoverityDefect implements Serializable {

    private String user;
    private String project;
    private Date timeStamp;
    private MergedDefect mergedDefect;
    private String latestSnapshotId;
    private StreamDefects streamDefects;
    private CheckerProperties checkerProperties;


    public Date getTimeStamp() {
        return timeStamp;
    }

    @XmlElement
    public void setTimeStamp(Date timeStamp) {
        this.timeStamp = timeStamp;
    }

    public String getUser() {
        return user;
    }

    @XmlElement
    public void setUser(String user) {
        this.user = user;
    }

    public String getProject() {
        return project;
    }

    @XmlElement
    public void setProject(String project) {
        this.project = project;
    }

    public MergedDefect getMergedDefect() {
        return mergedDefect;
    }

    @XmlElement
    public void setMergedDefect(MergedDefect mergedDefect) {
        this.mergedDefect = mergedDefect;
    }

    public String getLatestSnapshotId() {
        return latestSnapshotId;
    }

    @XmlElement
    public void setLatestSnapshotId(String latestSnapshotId) {
        this.latestSnapshotId = latestSnapshotId;
    }

    public StreamDefects getStreamDefects() {
        return streamDefects;
    }

    @XmlElement
    public void setStreamDefects(StreamDefects streamDefects) {
        this.streamDefects = streamDefects;
    }

    public CheckerProperties getCheckerProperties() {
        return checkerProperties;
    }

    @XmlElement
    public void setCheckerProperties(CheckerProperties checkerProperties) {
        this.checkerProperties = checkerProperties;
    }
}

其中一个失败的元素是xml中的mergedDefect,模型为:

package com.ericsson.integration.jira.jaxb.model;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * Created by olgunkaya on 11.09.2014.
 */

public class MergedDefect implements Serializable{

    private String checkerName;
    private String checkerSubcategory;
    private String cid;
    private String componentName;
    private List<DefectStateAttributeValues> defectStateAttributeValues;
    private String domain;
    private String filePathname;
    private Date firstDetected;
    private String firstDetectedSnapshotId;
    private String functionDisplayName;
    private Date lastDetected;
    private String lastDetectedSnapshotId;
    private Date lastFixed;
    private Date lastTriaged;
    private String mergeKey;
    private String occurrenceCount;

    public String getCheckerName() {
        return checkerName;
    }

    @XmlElement
    public void setCheckerName(String checkerName) {
        this.checkerName = checkerName;
    }

    public String getCheckerSubcategory() {
        return checkerSubcategory;
    }

    @XmlElement
    public void setCheckerSubcategory(String checkerSubcategory) {
        this.checkerSubcategory = checkerSubcategory;
    }

    public String getCid() {
        return cid;
    }

    @XmlElement
    public void setCid(String cid) {
        this.cid = cid;
    }

    public String getComponentName() {
        return componentName;
    }

    @XmlElement
    public void setComponentName(String componentName) {
        this.componentName = componentName;
    }

    public List<DefectStateAttributeValues> getDefectStateAttributeValues() {
        return defectStateAttributeValues;
    }

    @XmlElement
    public void setDefectStateAttributeValues(List<DefectStateAttributeValues> defectStateAttributeValues) {
        this.defectStateAttributeValues = defectStateAttributeValues;
    }

    public String getDomain() {
        return domain;
    }

    @XmlElement
    public void setDomain(String domain) {
        this.domain = domain;
    }

    public String getFilePathname() {
        return filePathname;
    }

    @XmlElement
    public void setFilePathname(String filePathname) {
        this.filePathname = filePathname;
    }

    public Date getFirstDetected() {
        return firstDetected;
    }

    @XmlElement
    public void setFirstDetected(Date firstDetected) {
        this.firstDetected = firstDetected;
    }

    public String getFirstDetectedSnapshotId() {
        return firstDetectedSnapshotId;
    }

    @XmlElement
    public void setFirstDetectedSnapshotId(String firstDetectedSnapshotId) {
        this.firstDetectedSnapshotId = firstDetectedSnapshotId;
    }

    public String getFunctionDisplayName() {
        return functionDisplayName;
    }

    @XmlElement
    public void setFunctionDisplayName(String functionDisplayName) {
        this.functionDisplayName = functionDisplayName;
    }

    public Date getLastDetected() {
        return lastDetected;
    }

    @XmlElement
    public void setLastDetected(Date lastDetected) {
        this.lastDetected = lastDetected;
    }

    public String getLastDetectedSnapshotId() {
        return lastDetectedSnapshotId;
    }

    @XmlElement
    public void setLastDetectedSnapshotId(String lastDetectedSnapshotId) {
        this.lastDetectedSnapshotId = lastDetectedSnapshotId;
    }

    public Date getLastFixed() {
        return lastFixed;
    }

    @XmlElement
    public void setLastFixed(Date lastFixed) {
        this.lastFixed = lastFixed;
    }

    public Date getLastTriaged() {
        return lastTriaged;
    }

    @XmlElement
    public void setLastTriaged(Date lastTriaged) {
        this.lastTriaged = lastTriaged;
    }

    public String getMergeKey() {
        return mergeKey;
    }

    @XmlElement
    public void setMergeKey(String mergeKey) {
        this.mergeKey = mergeKey;
    }

    public String getOccurrenceCount() {
        return occurrenceCount;
    }

    @XmlElement
    public void setOccurrenceCount(String occurrenceCount) {
        this.occurrenceCount = occurrenceCount;
    }
}

上面的mergedDefect对象始终返回null。 我正在使用下面的部分进行测试。

System.out.println(“从XML创建对象”);

    File file = new File("/Users/olgunkaya/Downloads/atlassian-jira-rest-java-client-890e21ab0a7b/" +
                         "jira-integration/resources/cov-export5653165670998483522.xml");

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    CoverityDefect coverityDefectt = (CoverityDefect) jaxbUnmarshaller.unmarshal(file);
    System.out.println(coverityDefectt);

1 个答案:

答案 0 :(得分:0)

感谢这篇文章的旧答案。 How to make JAXB unmarshaller to ignore prefixes?

我使用相同的方法删除所有前缀。现在它正在发挥作用。 简单地解决这样的问题。可以扩展XMLFilterImpl和覆盖startElement和endElement方法。然后,您可以修改传递给此方法的qName参数。实施可能会根据您的需要而有所不同。

您还应该小心使用POJO来映射xml。你可能有一些Sax Exceptions。

在这种情况下,您可能需要稍微使用xmlfilter实现。