问:可以帮助我解决这个问题。我正在尝试将xml转换为json,但是json的值与xml不同,因此值不匹配。这是片段
@SuppressWarnings("unused")
public static void main (String args[]) throws IOException{
int PRETTY_PRINT_INDENT_FACTOR = 1;
File file=new File("D:\\Input\\");
File[] allFiles=file.listFiles();
for(int i=0;i<allFiles.length;i++){
if(allFiles[i].getAbsolutePath().endsWith("xml")){
InputStream inputStream=new FileInputStream(allFiles[i]);
StringBuilder builder = new StringBuilder();
int ptr = 0;
while ((ptr = inputStream.read()) != -1 )
{
builder.append((char) ptr);
}
String xml = builder.toString();
JSONObject jsonObj = XML.toJSONObject(xml);
String jsonString=jsonObj.toString(PRETTY_PRINT_INDENT_FACTOR);
System.out.println();
byte[] contentBytes=jsonString.getBytes();
System.out.println(jsonString);
File f=new File("D:\\Important_Docs\\filewrite.txt");
FileOutputStream fop=new FileOutputStream(f);
fop.write(contentBytes);
}
}
}
答:public String getDate(){String data =“ Hello World”; 返回数据;}