Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java XStream使用toXML缺少换行符(对象,StreamWriter)_Java_Newline_Xstream - Fatal编程技术网

Java XStream使用toXML缺少换行符(对象,StreamWriter)

Java XStream使用toXML缺少换行符(对象,StreamWriter),java,newline,xstream,Java,Newline,Xstream,我尝试使用Xstream序列化Java中的对象列表,我的核心代码是: //register alias xstream.alias("category", ServerCategory.class); //register converter xstream.registerConverter(new ServerCategoryConverter()); // get all categories List<ServerCategory> categoryList = cate

我尝试使用Xstream序列化Java中的对象列表,我的核心代码是:

//register alias
xstream.alias("category", ServerCategory.class);

//register converter
xstream.registerConverter(new ServerCategoryConverter());

// get all categories
List<ServerCategory> categoryList = categoryDao.getAllEager();

// serialize all categories
for(ServerCategory category : categoryList) {
    xstream.toXML(category, streamWriter);
}
//寄存器别名
别名(“category”,ServerCategory.class);
//寄存器转换器
registerConverter(新的ServerCategoryConverter());
//获取所有类别
List categoryList=categoryDao.getAllEager();
//序列化所有类别
用于(服务器类别:categoryList){
toXML(类别,streamWriter);
}
这很好,但是在
-标记之后没有换行符,这似乎与xml规范冲突,因为每个读卡器都在这一行换行。
是否对此进行了修复?

您当前的代码将生成如下内容:

<category>
    <id>1</id>
    <name>One</name>
    <description>One</description>
    <syncversion>V1</syncversion>
    <parents/>
  </category>
  <category>
    <id>2</id>
    <name>Two</name>
    <description>Two</description>
    <syncversion>V1</syncversion>
    <parents>
      <parent>1</parent>
    </parents>
  </category>
您应该序列化整个列表

xstream.toXML(categoryList, streamWriter);
您的ServerCategoryConverter CanCover()应该被

public boolean canConvert(Class foo) {
            //dont use this converter for the Arraylist. Arraylist will be handeld by the default converter
            if (foo.getName().equals("java.util.ArrayList")) {
                return false;
            }
            return true;
        }
我已附上我的完整代码以供参考。它与上述修复程序配合使用:

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class XSTReamTest {
    /*
 * ExpertService.class
 */
    public static void main(String args[]) throws Exception {
        XSTReamTest tst = new XSTReamTest();
        tst.createXmlExport();
    }

    public File createXmlExport() throws Exception {
        XStream xstream = new XStream();
        File xmlExportFile = null;
        BufferedOutputStream outputStream = null;
        OutputStreamWriter streamWriter = null;

        try {
            xmlExportFile = new File("easylearncards1_exportRequest_.xml");
            System.out.println("Path = " + xmlExportFile.getAbsolutePath());

            outputStream = new BufferedOutputStream(new FileOutputStream(xmlExportFile));
            streamWriter = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));
            exportCategories(xstream, streamWriter);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                streamWriter.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return xmlExportFile;
    }

    private void exportCategories(XStream xstream, OutputStreamWriter streamWriter) {
        // register alias
        xstream.alias("category", ServerCategory.class);

        // register converter
        xstream.registerConverter(new ServerCategoryConverter());

        // get all categories
        List<ServerCategory> categoryList = new ArrayList<ServerCategory>();

        ServerCategory root = new ServerCategory("1", "One", "One", "V1", null);
        ServerCategory child1 = new ServerCategory("2", "Two", "Two", "V1", new ServerCategory[]{root});
        ServerCategory child2 = new ServerCategory("3", "Three", "Three", "V1", new ServerCategory[]{root});
        ServerCategory child3 = new ServerCategory("4", "Four", "Four", "V1", new ServerCategory[]{child1, child2});

        categoryList.add(root);
        categoryList.add(child1);
        categoryList.add(child2);
        categoryList.add(child3);

        // convert all categories
//        for (ServerCategory category : categoryList) {
//            xstream.toXML(category, streamWriter);
//        }
        xstream.toXML(categoryList, streamWriter);
    }

    static class ServerCategory {
        String id;
        String name;
        String description;
        String syncVersion;
        ServerCategory parents[];

        ServerCategory(String id, String name, String description, String syncVersion, ServerCategory[] parents) {
            this.id = id;
            this.name = name;
            this.description = description;
            this.syncVersion = syncVersion;
            this.parents = parents;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getSyncVersion() {
            return syncVersion;
        }

        public void setSyncVersion(String syncVersion) {
            this.syncVersion = syncVersion;
        }

        public ServerCategory[] getParents() {
            return parents;
        }

        public void setParents(ServerCategory[] parents) {
            this.parents = parents;
        }
    }
/*
 * ServerCategoryConverter.class
 */

    static class ServerCategoryConverter implements Converter {

        public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {

            ServerCategory category = (ServerCategory) value;

            writer.startNode("id");
            writer.setValue(category.getId().toString());
            writer.endNode();

            writer.startNode("name");
            writer.setValue(category.getName());
            writer.endNode();

            writer.startNode("description");
            writer.setValue(category.getDescription());
            writer.endNode();

            writer.startNode("syncversion");
            writer.setValue(category.getSyncVersion().toString());
            writer.endNode();

            writer.startNode("parents");
            if (category.getParents() != null) {
                for (ServerCategory parent : category.getParents()) {
                    writer.startNode("parent");
                    writer.setValue(parent.getId().toString());
                    writer.endNode();

                }
            }
            writer.endNode();

        }

        public java.lang.Object unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader hierarchicalStreamReader, com.thoughtworks.xstream.converters.UnmarshallingContext unmarshallingContext) {
            return null;
        }

        public boolean canConvert(Class foo) {
            if (foo.getName().equals("java.util.ArrayList")) {
                return false;
            }
            return true;
        }

    }
}
import com.thoughtworks.xstream.xstream;
导入com.thoughtworks.xstream.converters.Converter;
导入com.thoughtworks.xstream.converters.MarshallingContext;
导入com.thoughtworks.xstream.io.HierarchycalStreamWriter;
导入java.io.*;
导入java.nio.charset.charset;
导入java.util.ArrayList;
导入java.util.List;
公共类XSTReamTest{
/*
*ExpertService.class
*/
公共静态void main(字符串args[])引发异常{
XSTReamTest tst=新的XSTReamTest();
tst.createXmlExport();
}
公共文件createXmlExport()引发异常{
XStream XStream=新的XStream();
文件xmlExportFile=null;
BufferedOutputStream outputStream=null;
OutputStreamWriter streamWriter=null;
试一试{
xmlExportFile=新文件(“easylearncards1_exportRequest_u2;.xml”);
System.out.println(“Path=“+xmlExportFile.getAbsolutePath());
outputStream=新的BufferedOutputStream(新文件outputStream(xmlExportFile));
streamWriter=新的OutputStreamWriter(outputStream,Charset.forName(“UTF-8”));
导出类别(xstream、streamWriter);
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
streamWriter.close();
outputStream.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回xmlExportFile;
}
私有void导出类别(XStream XStream、OutputStreamWriter streamWriter){
//注册别名
别名(“category”,ServerCategory.class);
//寄存器转换器
registerConverter(新的ServerCategoryConverter());
//获取所有类别
List categoryList=新建ArrayList();
ServerCategory root=新的ServerCategory(“1”、“一”、“一”、“V1”,null);
ServerCategory child1=新的ServerCategory(“2”、“2”、“2”、“V1”,新的ServerCategory[]{root});
ServerCategory child2=新的ServerCategory(“3”、“三”、“三”、“V1”,新的ServerCategory[]{root});
ServerCategory child3=新的ServerCategory(“4”、“4”、“4”、“V1”、新的ServerCategory[]{child1,child2});
categoryList.add(根目录);
添加类别列表(child1);
添加类别列表(child2);
添加类别列表(child3);
//转换所有类别
//用于(服务器类别:categoryList){
//toXML(类别,streamWriter);
//        }
toXML(categoryList,streamWriter);
}
静态类ServerCategory{
字符串id;
字符串名;
字符串描述;
字符串同步;
ServerCategory父类[];
ServerCategory(字符串id、字符串名称、字符串描述、字符串同步版本、ServerCategory[]父级){
this.id=id;
this.name=名称;
this.description=描述;
this.syncVersion=syncVersion;
这个。父母=父母;
}
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getDescription(){
返回说明;
}
公共void集合描述(字符串描述){
this.description=描述;
}
公共字符串getSyncVersion(){
返回同步;
}
公共无效设置同步版本(字符串同步版本){
this.syncVersion=syncVersion;
}
公共服务器类别[]getParents(){
返回父母;
}
public void setParents(ServerCategory[]parents){
这个。父母=父母;
}
}
/*
*ServerCategoryConverter.class
*/
静态类ServerCategoryConverter实现转换器{
公共无效封送处理(对象值、HierarchycalStreamWriter编写器、封送上下文){
ServerCategory=(ServerCategory)值;
作者:startNode(“id”);
writer.setValue(category.getId().toString());
writer.endNode();
作者:startNode(“名称”);
writer.setValue(category.getName());
writer.endNode();
作者:startNode(“描述”);
writer.setValue(category.getDescription());
writer.endNode();
writer.startNode(“同步版本”);
writer.setValue(category.getSyncVersion().toString());
writer.endNode();
作者:startNode(“家长”);
if(category.getParents()!=null){
对于(ServerCategory父项:category.getParents()){
作者:startNode(“母公司”);
writer.setValue(parent.getId().toString());
writer.endNode();
}
}
public boolean canConvert(Class foo) {
            //dont use this converter for the Arraylist. Arraylist will be handeld by the default converter
            if (foo.getName().equals("java.util.ArrayList")) {
                return false;
            }
            return true;
        }
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class XSTReamTest {
    /*
 * ExpertService.class
 */
    public static void main(String args[]) throws Exception {
        XSTReamTest tst = new XSTReamTest();
        tst.createXmlExport();
    }

    public File createXmlExport() throws Exception {
        XStream xstream = new XStream();
        File xmlExportFile = null;
        BufferedOutputStream outputStream = null;
        OutputStreamWriter streamWriter = null;

        try {
            xmlExportFile = new File("easylearncards1_exportRequest_.xml");
            System.out.println("Path = " + xmlExportFile.getAbsolutePath());

            outputStream = new BufferedOutputStream(new FileOutputStream(xmlExportFile));
            streamWriter = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));
            exportCategories(xstream, streamWriter);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                streamWriter.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return xmlExportFile;
    }

    private void exportCategories(XStream xstream, OutputStreamWriter streamWriter) {
        // register alias
        xstream.alias("category", ServerCategory.class);

        // register converter
        xstream.registerConverter(new ServerCategoryConverter());

        // get all categories
        List<ServerCategory> categoryList = new ArrayList<ServerCategory>();

        ServerCategory root = new ServerCategory("1", "One", "One", "V1", null);
        ServerCategory child1 = new ServerCategory("2", "Two", "Two", "V1", new ServerCategory[]{root});
        ServerCategory child2 = new ServerCategory("3", "Three", "Three", "V1", new ServerCategory[]{root});
        ServerCategory child3 = new ServerCategory("4", "Four", "Four", "V1", new ServerCategory[]{child1, child2});

        categoryList.add(root);
        categoryList.add(child1);
        categoryList.add(child2);
        categoryList.add(child3);

        // convert all categories
//        for (ServerCategory category : categoryList) {
//            xstream.toXML(category, streamWriter);
//        }
        xstream.toXML(categoryList, streamWriter);
    }

    static class ServerCategory {
        String id;
        String name;
        String description;
        String syncVersion;
        ServerCategory parents[];

        ServerCategory(String id, String name, String description, String syncVersion, ServerCategory[] parents) {
            this.id = id;
            this.name = name;
            this.description = description;
            this.syncVersion = syncVersion;
            this.parents = parents;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getSyncVersion() {
            return syncVersion;
        }

        public void setSyncVersion(String syncVersion) {
            this.syncVersion = syncVersion;
        }

        public ServerCategory[] getParents() {
            return parents;
        }

        public void setParents(ServerCategory[] parents) {
            this.parents = parents;
        }
    }
/*
 * ServerCategoryConverter.class
 */

    static class ServerCategoryConverter implements Converter {

        public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {

            ServerCategory category = (ServerCategory) value;

            writer.startNode("id");
            writer.setValue(category.getId().toString());
            writer.endNode();

            writer.startNode("name");
            writer.setValue(category.getName());
            writer.endNode();

            writer.startNode("description");
            writer.setValue(category.getDescription());
            writer.endNode();

            writer.startNode("syncversion");
            writer.setValue(category.getSyncVersion().toString());
            writer.endNode();

            writer.startNode("parents");
            if (category.getParents() != null) {
                for (ServerCategory parent : category.getParents()) {
                    writer.startNode("parent");
                    writer.setValue(parent.getId().toString());
                    writer.endNode();

                }
            }
            writer.endNode();

        }

        public java.lang.Object unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader hierarchicalStreamReader, com.thoughtworks.xstream.converters.UnmarshallingContext unmarshallingContext) {
            return null;
        }

        public boolean canConvert(Class foo) {
            if (foo.getName().equals("java.util.ArrayList")) {
                return false;
            }
            return true;
        }

    }
}