在 java 中用 xstream 解析 xml(android 工作室)

Parsing xml with xstream in java (android studio)

出了点问题,应用程序崩溃了。我正在尝试将 xml 文本解析为 java 类,我正在使用 XStream 库。使用方法 fromXML 后代码崩溃。我的代码:

XML 文件如下所示:

<FilesManifest>
<Group>
    <HeaderName>Pirmas hdr</HeaderName>
    <Type>theory</Type>
    <Piece>
        <FileName>11.txt</FileName>
        <HeadLine>hi1</HeadLine>
    </Piece>
    <Piece>
        <FileName>22.txt</FileName>
        <HeadLine>hi2</HeadLine>
    </Piece>
</Group>

<Group>
    <HeaderName>antras hdr</HeaderName>
    <Type>theory</Type>
    <Piece>
        <FileName>33.txt</FileName>
        <HeadLine>hi3</HeadLine>
    </Piece>
    <Piece>
        <FileName>44.txt</FileName>
        <HeadLine>hi4</HeadLine>
    </Piece>
</Group>

<Group>
    <HeaderName>tracias hdr</HeaderName>
    <Type>test</Type>
    <Piece>
        <FileName>55.txt</FileName>
        <HeadLine>hi5</HeadLine>
    </Piece>
    <Piece>
        <FileName>66.txt</FileName>
        <HeadLine>hi6</HeadLine>
    </Piece>
</Group>
</FilesManifest>

我的主要功能代码:

String fileText = ReadFile();
XStream xstream = new XStream();

xstream.alias("FilesManifest", MyFileManager.class);
xstream.alias("Group", MyGroup.class);
xstream.alias("Piece", MyPiece.class);

MyFileManager data = (MyFileManager)xstream.fromXML(fileText);

和类用于xml解析:

public class MyFileManager {
    public List<MyGroup> getGroups() {
        return groups;
    }

    public void setGroups(List<MyGroup> groups) {
        this.groups = groups;
    }
    @XStreamImplicit(itemFieldName = "group")
    private List<MyGroup> groups = new ArrayList<MyGroup>();
}

public class MyGroup {
    private String headerName;
    private String type;

    @XStreamImplicit(itemFieldName = "piece")
    private List<MyPiece> pieces = new ArrayList<MyPiece>();

    public List<MyPiece> getPieces() {
        return pieces;
    }

    public void setPieces(List<MyPiece> pieces) {
        this.pieces = pieces;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getHeaderName() {
        return headerName;
    }

    public void setHeaderName(String headerName) {
        this.headerName = headerName;
    }

}

public class MyPiece {
    private String fileName;
    private String headLine;

    public String getHeadLine() {
        return headLine;
    }

    public void setHeadLine(String headLine) {
        this.headLine = headLine;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
}

那么你能告诉我代码可以正常工作吗?也许还有其他解析方式?

我可以通过解决 2 个主要问题使您的代码正常工作:

  1. 您的映射不完整且不正确,因为您似乎忘记了 XMLcase-sensitive 这意味着例如 @XStreamImplicit(itemFieldName = "group") 应该是 @XStreamImplicit(itemFieldName = "Group") 并且所有字段必须用 @XStreamAlias 注释以提供第一个字符大写的别名。
  2. 您没有调用 processAnnotations(Class type),因此您的注释未被处理。

所以最后你的代码应该是这样的:

您的 POJO:

public class MyFileManager {
    ...
    @XStreamImplicit(itemFieldName = "Group")
    private List<MyGroup> groups = new ArrayList<>();
}

public class MyGroup {
    @XStreamAlias("HeaderName")
    private String headerName;
    @XStreamAlias("Type")
    private String type;

    @XStreamImplicit(itemFieldName = "Piece")
    private List<MyPiece> pieces = new ArrayList<>();

    ...
}

public class MyPiece {
    @XStreamAlias("FileName")
    private String fileName;
    @XStreamAlias("HeadLine")
    private String headLine;

    ...
}

您的主要功能代码:

...
// Process the provided annotations
xstream.processAnnotations(MyFileManager.class);
MyFileManager data = (MyFileManager)xstream.fromXML(fileText);