我正在使用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());
}
}
答案 0 :(得分:0)
路径:/ 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>
答案 1 :(得分:0)
xstream.alias("objects",Objects.class);
这会告诉XStream
在xml中遇到Object
标记时创建Objects
类型<objects>
。
你没有告诉它它应该为<object>
做什么,所以它(我认为)创造了它所知道的那个,即。 java.lang.Object
。
正如我在评论中提到的,也许你需要
xstream.alias("object", Your.Package.Object.class);