java对象转换成xml,javascript对象字符串, xml转换成java对象,支持任意类型,支持深层转换

/*
 * $RCSfile: TestJ2X.java,v $
 * $Revision: 1.1  $
 * $Date: 2007-6-7  $
 */

package test.org.j2x.xml;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.dom4j.DocumentException;
import org.j2x.test.TestObject;
import org.j2x.util.Dom4jUtil;
import org.j2x.util.Equal;
import org.j2x.util.IO;
import org.j2x.util.TestUtil;
import org.j2x.xml.Xml2Java;

/**
 *  
 * <p>Title: TestJ2X</p> 
 * <p>Description: </p> 
 * @author chenyankui
 * @version 1.0
 */
public class TestJ2X
{
    /**
     * @param args - void
     * @author: chenyankui
     */
    public static void main(String[] args)
    {
        String java2Xml = "./test/Java2XmlTestObject.xml";
        String xml2Java = "./test/Xml2JavaTestObject.xml";
        
        TestUtil.showRuler(150);
        
        /**
         * 如果生成的两份文件完全一样,则证明互相转换是成功的
         */
        testJ2X(TestObject.createTestObject(), java2Xml, xml2Java);
        
        try
        {
            boolean isEqual = Dom4jUtil.load(java2Xml, "GBK").equals(Dom4jUtil.load(xml2Java, "GBK"));
            
            /**
             * 此处一般打印false,证明不相等
             * 这是因为TestObject对象内部存在Map对象,Map对象内的元素没有顺序导致了两份文件结构不一致,从而导致两个document对象也不一致
             */
            System.out.println("equals: " + isEqual);
        }
        catch(DocumentException e)
        {
            e.printStackTrace();
        }
        
        Object o1 = new Xml2Java().read(java2Xml);
        Object o2 = new Xml2Java().read(xml2Java);
        
        System.out.println("equals: " + Equal.equals(o1, o2));
    }
    
    /**
     * 验证测试
     * @param java2Xml
     * @param xml2Java
     */
    public static void testJ2X(Object object, String java2Xml, String xml2Java)
    {
        if(object == null)
        {
            System.out.println("[Test Object]: null");
        }
        else
        {
            System.out.println("[Test Object]: " + object.getClass().getName());
        }
        
        testJava2Xml(object, java2Xml);
        testXml2Java(object, xml2Java);
    }
    
    /**
     * 将对象转换成xml并保存在指定路径下
     * @param testObject
     * @param savePath
     */
    public static void testJava2Xml(Object testObject, String savePath)
    {
        String xml = TestUtil.java2Xml(testObject);
        
        System.out.println("Xml:/r/n" + xml);
        
        try
        {
            IO.copy(new ByteArrayInputStream(xml.getBytes()), savePath);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
    
    /**
     * 将给定的xml转换成对象然后再将对象转换成xml并保存在指定路径下
     * @param testObject
     * @param savePath
     */
    public static void testXml2Java(Object testObject, String savePath)
    {
        String xml = TestUtil.java2Xml(testObject);
        
        System.out.println("Xml:" + xml);
        
        Object object = TestUtil.xml2Java(xml); 
        
        testJava2Xml(object, savePath);
    }
}
 /*
 * $RCSfile: TestReader.java,v $
 * $Revision: 1.1  $
 * $Date: 2007-6-8  $
 */

package test.org.j2x.xml;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.j2x.util.TestUtil;

/**
 *  
 * <p>Title: TestInputStream</p> 
 * <p>Description: </p> 
 * @author chenyankui
 * @version 1.0
 */
public class TestInputStream
{
    /**
     * @param args - void
     * @author: chenyankui
     */
    public static void main(String[] args)
    {
        TestUtil.showRuler(150);
        
        testInputStream();
    }
    
    public static void testInputStream()
    {
        InputStream stream1 = new ByteArrayInputStream("You are a fool man".getBytes());
        InputStream stream2 = new ByteArrayInputStream("You are a fool man".getBytes());
        
        /**
         * reader1被读取完毕
         */
        TestJ2X.testJava2Xml(stream1, "./test/Java2XmlInputStream.xml");
        
        /**
         * 需要新定义的reader2,reader1已经没有数据
         */
        TestJ2X.testXml2Java(stream2, "./test/Xml2JavaInputStream.xml");
    }
}
/*
 * $RCSfile: TestList.java,v $
 * $Revision: 1.1  $
 * $Date: 2007-6-8  $
 */

package test.org.j2x.xml;

import java.util.ArrayList;
import java.util.List;

/**
 *  
 * <p>Title: TestList</p> 
 * <p>Description: </p> 
 * @author chenyankui
 * @version 1.0
 */
public class TestList
{
    /**
     * @param args - void
     * @author: chenyankui
     */
    public static void main(String[] args)
    {
        testList();
    }
    
    public static void testList()
    {
        List list = new ArrayList();
        
        list.add("白日依山尽,");
        list.add("黄河入海流,");
        list.add("欲穷千里目,");
        list.add("更上一层楼.");
        
        List childList = new ArrayList();
        
        childList.add("好诗!");
        childList.add("真正的好诗!");
        
        list.add(childList);
        
        TestJ2X.testJ2X(list, "./test/Java2XmlList.xml", "./test/Xml2JavaList.xml");
    }
}
/*
 * $RCSfile: TestReadObject.java,v $
 * $Revision: 1.1  $
 * $Date: 2007-6-8  $
 */

package test.org.j2x.xml;

import java.io.File;
import org.j2x.util.TestUtil;
import org.j2x.xml.Xml2Java;

/**
 * 
 * <p>Title: TestReadObject</p> 
 * <p>Description: </p> 
 * @author chenyankui
 * @version 1.0
 */
public class TestReadObject
{
    /**
     * @param args - void
     * @author: chenyankui
     */
    public static void main(String[] args)
    {
        testReadObjectFromXml();
    }
    
    public static void testReadObjectFromXml()
    {
        File file = new File("./test/");
        
        File[] list = file.listFiles();
        
        TestUtil.showRuler(150);
        
        String indent = "    ";
        
        for(int i = 0; i < list.length; i++)
        {
            if(list[i].exists() && list[i].isFile())
            {
                System.out.println("[--------------------------------------------------] " + i);
                
                Object object = new Xml2Java().read(list[i]);
                
                if(object != null)
                {
                    System.out.println(indent + list[i].getAbsolutePath());
                    System.out.println(indent + "[Object]:" + object.getClass().getName());
                    System.out.println(indent + "[Value ]:" + object.toString());
                }
                else
                {
                    System.out.println(indent + list[i].getAbsolutePath());
                    System.out.println(indent + "[Object]: null");
                }
                
                System.out.println("[--------------------------------------------------] " + i);
                System.out.println();
            }
        }
    }
}
/*
 * $RCSfile: TestReturnValue.java,v $
 * $Revision: 1.1  $
 * $Date: 2007-6-8  $
 */

package test.org.j2x.xml;

import org.j2x.util.ReturnValue;
import org.j2x.util.TestUtil;

/**
 * 
 * <p>Title: TestReturnValue</p> 
 * <p>Description: </p> 
 * @author chenyankui
 * @version 1.0
 */
public class TestReturnValue
{
    public static void main(String[] args)
    {
        TestUtil.printAsXml(new ReturnValue(0, "You are right !"));
    }
}
/*
 * $RCSfile: TestStringArray.java,v $
 * $Revision: 1.1  $
 * $Date: 2007-6-8  $
 */

package test.org.j2x.xml;

/**
 *  
 * <p>Title: TestStringArray</p> 
 * <p>Description: </p> 
 * @author chenyankui
 * @version 1.0
 */
public class TestStringArray
{
    /**
     * @param args - void
     * @author: chenyankui
     */
    public static void main(String[] args)
    {
        test();
    }
    
    public static void test()
    {
        String[] array = new String[]{"string1", "string2"};
        
        /**
         * reader1被读取完毕
         */
        TestJ2X.testJava2Xml(array, "./test/Java2XmlStringArray.xml");
        
        /**
         * 需要新定义的reader2,reader1已经没有数据
         */
        TestJ2X.testXml2Java(array, "./test/Xml2JavaStringArray.xml");
    }
}
/*
 * $RCSfile: TestList.java,v $
 * $Revision: 1.1  $
 * $Date: 2007-6-8  $
 */

package test.org.j2x.xml;

import java.util.Vector;

/**
 *  
 * <p>Title: TestVector</p> 
 * <p>Description: </p> 
 * @author chenyankui
 * @version 1.0
 */
public class TestVector
{
    /**
     * @param args - void
     * @author: chenyankui
     */
    public static void main(String[] args)
    {
        testVector();
    }
    
    /**
     * 
     *  - void
     * @author: chenyankui
     */
    public static void testVector()
    {
        Vector vertor = new Vector();
        
        vertor.add("大江东去,浪,淘尽千古风流人物!");
        vertor.add("故垒西边,人道是,三国周郎赤壁!");
        vertor.add("其实不是 !");
        
        TestJ2X.testJ2X(vertor, "./test/Java2XmlVector.xml", "./test/Xml2JavaVector.xml");
    }
}
/*
 * $RCSfile: TestXml2Java.java,v $
 * $Revision: 1.1  $
 * $Date: 2007-6-6  $
 */

package test.org.j2x.xml;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.j2x.test.TestObject;
import org.j2x.util.IO;
import org.j2x.util.TestUtil;
import org.j2x.xml.Java2Xml;

/**
 *  
 * <p>Title: TestXml2Java</p> 
 * <p>Description: </p> 
 * @author chenyankui
 * @version 1.0
 */
public class TestXml2Java
{
    /**
     * @param args - void
     * @author: chenyankui
     */
    public static void main(String[] args)
    {
        String java2Xml = "E://Temp//java2xml.xml";
        String xml2Java = "E://Temp//xml2java.xml";
        
        /**
         * 如果生成的两份文件完全一样,则证明互相转换是成功的
         */
        testJ2X(java2Xml, xml2Java);
    }
    
    /**
     * 验证测试
     * @param java2Xml
     * @param xml2Java
     */
    public static void testJ2X(String java2Xml, String xml2Java)
    {
        // TestObject object = TestObject.createTestObject();
        
        List object = new ArrayList();
        
        object.add("123");
        object.add("456");
        
        testJava2Xml(object, java2Xml);
        testXml2Java(object, xml2Java);
    }
    
    /**
     * 将对象转换成xml并保存在指定路径下
     * @param testObject
     * @param savePath
     */
    public static void testJava2Xml(Object testObject, String savePath)
    {
        String xml = TestUtil.java2Xml(testObject);
        
        try
        {
            IO.copy(new ByteArrayInputStream(xml.getBytes()), savePath);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
    
    /**
     * 将给定的xml转换成对象然后再将对象转换成xml并保存在指定路径下
     * @param testObject
     * @param savePath
     */
    public static void testXml2Java(Object testObject, String savePath)
    {
        String xml = TestUtil.java2Xml(testObject);
        
        Object object = TestUtil.xml2Java(xml);
        
        testJava2Xml(object, savePath);
    }
    
    /**
     * 将对象转换成Document
     * @return
     */
    public static Document getTestDocument()
    {
        TestObject object = TestObject.createTestObject();
        
        Java2Xml java2Xml = new Java2Xml();
        
        return java2Xml.toDocument("Object", object);
    }
}
/*
 * $RCSfile: TestJsonUtil.java,v $
 * $Revision: 1.1  $
 * $Date: 2007-5-30  $
 */

package test.org.j2x.jso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.j2x.test.TestObject;
import org.j2x.util.TestUtil;

/**
 *  
 * <p>Title: TestJsonUtil</p> 
 * <p>Description: </p> 
 * @author chenyankui
 * @version 1.0
 */
public class TestJava2Jso
{
    public static void main(String[] args)
    {
        testObject();
    }
    
    /**
     * 
     *  - void
     * @author: chenyankui
     */
    public static void testObject()
    {
        TestObject object = TestObject.createTestObject();
        
        object.getMyList().add(new TestObject());
        
        object.setMyIntArray(new int[]{5, 6, 7, 8, 9, 10, 11, 12, 15, 16, 17, 18, 19, 20});
        
        object.getMyMap().put(new TestObject(), "123");
        object.getMyMap().put("456", new TestObject());
        object.getMyMap().put("789", "abc");
        
        TestUtil.printAsJson(object);
    }
    
    public static void testNull()
    {
        Object object = null;
        
        TestUtil.printAsJson(object, null, null);
    }
    
    /**
     * 
     *  - void
     * @author: chenyankui
     */
    public static void testByte()
    {
        Byte oByte = new Byte((byte)5);
        
        TestUtil.printAsJson(oByte, null, null);
    }
    
    /**
     * 
     *  - void
     * @author: chenyankui
     */
    public static void testDate()
    {
        TestUtil.printAsJson(new java.util.Date(), null, null);
    }
    
    /**
     * 
     *  - void
     * @author: chenyankui
     */
    public static void testList()
    {
        List list = new ArrayList();
        
        list.add(new Integer(1));
        list.add(new Double(2.3D));
        list.add("Hello ");
        list.add("You are a fool man !");
        list.add(new int[]{1, 2, 3});
        
        TestUtil.printAsJson(list);
    }
    
    /**
     * 
     *  - void
     * @author: chenyankui
     */
    public static void testMap()
    {
        Map map = new HashMap();
        
        map.put("a", "This a character");
        map.put("b", "This a character");
        map.put("c", "This a character");
        
        map.put(new TestObject(), "This a TestObject");
        
        TestUtil.printAsJson(map);
    }
    
    public static void testCRLF()
    {
        TestUtil.printAsJson("/r/n/b/t/f");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值