代码之家  ›  专栏  ›  技术社区  ›  Aditya Kaushik

XStream错误解决方案

  •  0
  • Aditya Kaushik  · 技术社区  · 11 年前

    我正在使用一个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)
    

    这是因为我的代码包含一个具有成员名称的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());
    
        }
        }
    
    2 回复  |  直到 11 年前
        1
  •  0
  •   Miserable Variable    11 年前
    xstream.alias("objects",Objects.class);
    

    这告诉 XStream 创建 Object 类型为 Objects 当它遇到 <objects> 标记。

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

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

    xstream.alias("object", Your.Package.Object.class);
    
        2
  •  0
  •   Pradyumna Das    11 年前

    路径:/objects/object/name

    我认为xml的根应该是 <objects> 标签例如

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