0

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

XML是:

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

解析时出现以下错误:

Exception in thread "main" com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field java.lang.Object.name
---- Debugging information ----
field               : name
class               : java.lang.Object
required-type       : java.lang.Object
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /objects/object/name
class[1]            : compareObjects.input.Objects
version             : null
-------------------------------
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.determineType(AbstractReflectionConverter.java:453)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:294)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:322)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1058)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1042)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:913)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:904)
    at compareObjects.Logic.mainClass.main(mainClass.java:60)

这是因为我的代码包含一个带有成员 name 的 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());

    }
    }
4

2 回答 2

0

路径:/对象/对象/名称

我认为你的 xml 的根需要是<objects>标签。例如

<objects>
    <object>
      <name>an Object Name</name>
      <ncss>154</ncss>
      <functions>48</functions>
      <classes>1</classes>
      <javadocs>44</javadocs>
    </object>
</objects>
于 2013-04-11T06:08:50.463 回答
0
xstream.alias("objects",Objects.class);

这告诉它在 xml 中遇到标记时XStream创建一个Object类型。Objects<objects>

你没有告诉它它应该做什么<object>,所以它(我认为)创建它知道的那个,即。java.lang.Object.

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

xstream.alias("object", Your.Package.Object.class);
于 2013-04-12T18:00:09.017 回答