Java XStream错误解析

Java XStream错误解析,java,parsing,xstream,Java,Parsing,Xstream,我正在使用java应用程序使用XStream解析一些XML XML是: <object> <name>an Object Name</name> <ncss>154</ncss> <functions>48</functions> <classes>1</classes> <javadocs>44</javado

我正在使用java应用程序使用XStream解析一些XML

XML是:

<object>
      <name>an Object Name</name>
      <ncss>154</ncss>
      <functions>48</functions>
      <classes>1</classes>
      <javadocs>44</javadocs>
    </object>
这是因为我的代码包含一个带有成员名的对象类,但JDK引用的是java.lang.Object类,它没有这样的字段

如何解决此冲突? 欢迎任何帮助。 提前谢谢

编辑:相同的代码为:

package compareObjects.Logic;


    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;

    import com.thoughtworks.xstream.XStream;
    import com.thoughtworks.xstream.io.xml.DomDriver;
    import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;

import compareObjects.input.*;
import compareObjects.outPut.changedObject;
import compareObjects.outPut.changedObjects;



    public class mainClass {

    public static void main(String[] args) throws IOException{

        BufferedReader br = new BufferedReader(new FileReader(new File("D:\\Object\\old_code_complexity.xml")));
        BufferedReader br2 = new BufferedReader(new FileReader(new File("D:\\Object\\new_code_complexity.xml")));

        String line;  //two strings to hold the old and new file's contents
        String line2;


        StringBuilder sb = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();


        //Objects representing the output to be stored in XML Format

        changedObjects oPack = new changedObjects();

        // remove multiple occurrences of space in the xml input file old_code_complexity.xml
        while((line=br.readLine())!= null){
            sb.append(line.trim());
        }

        String x = sb.toString();

     // remove multiple consecutive occurrences of spaces in contents of new_code_complexity.xml
        while((line2=br2.readLine())!= null){
            sb2.append(line2.trim());
        }

        //Create xstream instance to disable xstream from pushing extra _ character as escape sequence

        XStream xstream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("_-", " ")));

        // Create aliases to shorten class names in output file.


        xstream.alias("objects",Objects.class);

        Objects oList = (Objects)xstream.fromXML(x);

        String x2 = sb2.toString();
        Objects oList2 = (Objects)xstream.fromXML(x2);


        for(int i = 0; i< oList.getSize();i++)
        {
           for(int j = 0; j< oList2.getSize();j++)

        {
           if(oList.getObject(i).getName().equals(oList2.getObject(j).getName()))
           {

               if(oList.getObject(i).equals(oList2.getObject(j)))
                    {
                      oPack.addSimilarObject(((new changedObject("old_code_complexity.xml",oList.getObject(i),"new_code_complexity.xml",oList2.getObject(j)))));
                    }
            else
            {
                oPack.addChangedObject(((new changedObject("old_code_complexity.xml",oList.getObject(i),"new_code_complexity.xml",oList2.getObject(j)))));

            }

         }
          }



        }

        System.out.println(xstream.toXML(oPack));
        System.out.println("Objects Changed " + oPack.getDifferentObjects());
        System.out.println("Packages Not Changed" + oPack.getSimilarObjects());

    }
    }
包比较对象逻辑;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入com.thoughtworks.xstream.xstream;
导入com.thoughtworks.xstream.io.xml.DomDriver;
导入com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
导入比较对象。输入。*;
导入CompareObject.outPut.changedObject;
导入compareObjects.outPut.changedObjects;
公共类主类{
公共静态void main(字符串[]args)引发IOException{
BufferedReader br=new BufferedReader(新文件阅读器(新文件(“D:\\Object\\old\u code\u complexity.xml”));
BufferedReader br2=新的BufferedReader(新文件读取器(新文件(“D:\\Object\\new\u code\u complexity.xml”));
String line;//两个字符串用于保存旧文件和新文件的内容
弦线2;
StringBuilder sb=新的StringBuilder();
StringBuilder sb2=新的StringBuilder();
//对象,表示以XML格式存储的输出
changedObjects oPack=新的changedObjects();
//删除xml输入文件old_code_complexity.xml中多次出现的空格
而((line=br.readLine())!=null){
sb.append(line.trim());
}
字符串x=sb.toString();
//删除新_code_complexity.xml内容中多个连续出现的空格
而((line2=br2.readLine())!=null){
sb2.append(line2.trim());
}
//创建xstream实例以禁止xstream将额外的uu字符作为转义序列
XStream XStream=newXStream(新的DomDriver(“UTF-8”,新的XmlFriendlyNameCoder(“-”,”));
//创建别名以缩短输出文件中的类名。
别名(“objects”,objects.class);
Objects oList=(Objects)xstream.fromXML(x);
字符串x2=sb2.toString();
Objects oList2=(Objects)xstream.fromXML(x2);
对于(int i=0;i
路径:/objects/object/name 我认为xml的根需要是
标记。例如

<objects>
    <object>
      <name>an Object Name</name>
      <ncss>154</ncss>
      <functions>48</functions>
      <classes>1</classes>
      <javadocs>44</javadocs>
    </object>
</objects>

对象名
154
48
1.
44
这告诉
XStream
在遇到xml中的
标记时,创建类型为
Objects
对象

您没有告诉它应该为
做什么,所以它(我认为)正在创建一个它知道的,即
java.lang.Object

正如我在评论中提到的,也许你需要

xstream.alias("object", Your.Package.Object.class);

理想情况下,我会给自定义类一个不同的名称,以避免混淆。您可以发布相关代码吗?可能类似于
xstream.alias(“object”,you.Package.object.class)
?您告诉的是
XStream
什么是
,但不是
什么是。@非常遗憾,非常感谢您的建议。它解决了问题,很高兴它起了作用。我补充了一个你可以接受的答案。
xstream.alias("objects",Objects.class);
xstream.alias("object", Your.Package.Object.class);