如何使用多个命名空间解组此xml

时间:2016-06-13 09:08:32

标签: java xml xml-namespaces

  

我定义了两个用于解组xml的类,但我得到的结果总是为null;

@XmlRootElement(name="ValidateApiUserResponse", namespace="http://ops.net/AdminAPI_Admin")

公共类ValidateApiUserResult扩展了OrionResponse {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private ValidateApiUserResponse validateApiUserResult;

@XmlElement(name = "ValidateApiUserResult", namespace = DataStructuresNS.VanguardAdminNS)
public ValidateApiUserResponse getValidateApiUserResult() {
    return validateApiUserResult;
}

public void setValidateApiUserResult(ValidateApiUserResponse validateApiUserResult) {
    this.validateApiUserResult = validateApiUserResult;
}

}

公共类ValidateApiUserResponse实现了java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private String[] claims;
private Boolean isValid;
private int[] validServerIds;

public ValidateApiUserResponse() {
}

public ValidateApiUserResponse(String[] claims, Boolean isValid, int[] validServerIds) {
       this.claims = claims;
       this.isValid = isValid;
       this.validServerIds = validServerIds;
}

@XmlElement(name = "Claims", namespace = DataStructuresNS.VanguardAdminNS)
public String[] getClaims() {
    return claims;
}

public void setClaims(String[] claims) {
    this.claims = claims;
}

@XmlElement(name = "IsValid", namespace = DataStructuresNS.VanguardAdminNS)
public Boolean getIsValid() {
    return isValid;
}

public void setIsValid(Boolean isValid) {
    this.isValid = isValid;
}

@XmlElement(name = "ValidServerIds", namespace = DataStructuresNS.VanguardAdminNS)
public int[] getValidServerIds() {
    return validServerIds;
}

public void setValidServerIds(int[] validServerIds) {
    this.validServerIds = validServerIds;
}

}

  

这是一个命名空间

public class DataStructuresNS {

public static final String VanguardAdminNS = "http://schemas.datacontract.org/2004/07/Admin.DataStructures";

}

  

然后我尝试解组

String abc = "<ValidateApiUserResponse xmlns=\"http://ops.net/AdminAPI_Admin\">\n" +
    "         <ValidateApiUserResult xmlns:a=\"http://schemas.datacontract.org/2004/07/Admin.DataStructures\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
    "            <a:Claims xmlns:b=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">\n" +
    "               <b:string>Admin</b:string>\n" +
    "               <b:string>MANAGER</b:string>\n" +
    "            </a:Claims>\n" +
    "            <a:IsValid>true</a:IsValid>\n" +
    "            <a:ValidServerIds xmlns:b=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">\n" +
    "               <b:int>5001</b:int>\n" +
    "            </a:ValidServerIds>\n" +
    "         </ValidateApiUserResult>\n" +
    "      </ValidateApiUserResponse>";

    JAXBContext jaxbContext = JAXBContext.newInstance(ValidateApiUserResult.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    ValidateApiUserResult root = (ValidateApiUserResult)unmarshaller.unmarshal(new StringReader(abc));
  

我得到的根是空的

2 个答案:

答案 0 :(得分:0)

为您的根类尝试此操作:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="ValidateApiUserResponse", namespace="http://ops.net/AdminAPI_Admin")
public class ValidateApiUserResult extends OrionResponse { ... }

答案 1 :(得分:0)

  

我为数组定义了新类,并在@XmlElement中添加了type属性。     例如:

private OrionString[] claims;

@XmlElement(name = "Claims", namespace = "xxxxx", type=OrionString.class)
public OrionString[] getClaims() {
    return claims;
}
  

这是我定义的新课程

public class OrionString {

private String[] oString;

@XmlElement(name = "string", namespace = "http://schemas.microsoft.com/2003/10/Serialization/Arrays")
public String[] getoString() {
    return oString;
}

public void setoString(String[] oString) {
    this.oString = oString;
}

}