Java com.thoughtworks.xstream.io.xml.DomDriver 代码实例

・13 分钟阅读

以下是展示如何使用com.thoughtworks.xstream.io.xml.DomDriver的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


private static XStream configureXStream() {
    XStream xStream = new XStream(new DomDriver());
    xStream.alias("Sections", TestData.class);
    xStream.omitField(TestData.class, "loadedSections");
    xStream.addImplicitCollection(TestData.class, "sectionsLoadedFromFile", "Section", Section.class);
    xStream = Section.configurePageLayoutXStream(xStream);
    return xStream;
}
 

实例 2


/**
 * Constructor that takes a custom XStream object
 * @param xstream
 * @throws SerializationException
 */
public SimpleXStreamSerializer(XStream customXstream) throws SerializationException {
        if (customXstream == null) {
                xstream = new XStream(new DomDriver());
        } else {
                this.xstream = customXstream;
        }
        //this is added to read the prior simpleframework-serialized values.
        // TODO find a better way to do this.
        this.xstream.useAttributeFor(ImplementationId.class, "implementationId");
}
 

实例 3


public static void serializeXml( Object o , String fileName ) {
    XStream xstream = new XStream(new DomDriver());
    try {
        xstream.toXML(o,new FileOutputStream(fileName));
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }
}
 

实例 4


public OmBuilderXStreamImpl() 
{
    xstream = new XStream(new DomDriver());
    xstream.alias("applications", ArrayList.class);
    xstream.alias("application", PortletApplicationEntityImpl.class);
    xstream.alias("entities", ArrayList.class);
    xstream.alias("entity", PortletEntityImpl.class);
    xstream.alias("preferences", ArrayList.class);
    xstream.alias("preference", PreferenceImpl.class);
    //xstream.alias("name", java.lang.String.class);
    xstream.alias("value", java.lang.String.class);
    //xstream.alias("value", ArrayList.class);
    //xstream.addImplicitCollection(PreferenceSet.class, "preference",
    // PreferenceImpl.class);
    //        xstream.addImplicitCollection(
    //            ArrayList.class,
    //            "applications",
    //            PortletApplicationEntityImpl.class);
    xstream.addImplicitCollection(PreferenceImpl.class, "values", String.class);
}
 

实例 5


/**
 * 
 * @param metadata
 * @param fileName
 *            filename where serialise the metadata
 * @return true if serialisations is possible and went well. False if
 *         serialisation fails.
 */
public boolean serialize(TestsMetadata metadata, String fileName) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
                fos = new FileOutputStream(fileName);
                oos = new ObjectOutputStream(fos);
                oos.writeObject(metadata);
        } catch (Exception e) {
                e.printStackTrace();
                return false;
        } finally {
                try {
                        oos.close();
                } catch (Exception e) {
                }
                try {
                        fos.close();
                } catch (Exception e) {
                }
        }
        try {
                XStream xstream = new XStream(new DomDriver());
                fos = new FileOutputStream(fileName + ".debug");
                fos.write(xstream.toXML(metadata).getBytes());
        } catch (Exception e) {
                e.printStackTrace();
                return false;
        } finally {
                try {
                        fos.close();
                } catch (Exception e) {
                }
        }
        return true;
}
 

实例 6


public static void dumpObjects( Collection objects ) throws IOException {
    StringBuffer buffer = new StringBuffer();
    XStream xstream = new XStream(new DomDriver());
    buffer.append( xstream.toXML( objects ) );
    File tmp = File.createTempFile( "hua",".xml" );
    IOUtils.write(  buffer.toString(),new FileOutputStream( tmp ));
    System.out.println( tmp.getAbsolutePath() );
}
 

实例 7


public static String transformToXml(Object bean) {
                        XmlFriendlyReplacer replacer = new XmlFriendlyReplacer("_", "_");
                        XStream xstream = new XStream(new DomDriver("UTF-8", replacer)); 
                        xstream.setMode(XStream.NO_REFERENCES);
                        setAlias(xstream);      
                        String xml = xstream.toXML(bean);
                        return xml;
                }
 

实例 8


private ConfigPersister() {
        _xstream = new XStream(new DomDriver());
        _xstream.alias("launch4jConfig", Config.class);
        _xstream.alias("classPath", ClassPath.class);
        _xstream.alias("jre", Jre.class);
        _xstream.alias("splash", Splash.class);
        _xstream.alias("versionInfo", VersionInfo.class);
        _xstream.addImplicitCollection(Config.class, "headerObjects", "obj",
                        String.class);
        _xstream.addImplicitCollection(Config.class, "libs", "lib", String.class);
        _xstream.addImplicitCollection(Config.class, "variables", "var", String.class);
        _xstream.addImplicitCollection(ClassPath.class, "paths", "cp", String.class);
        _xstream.addImplicitCollection(Jre.class, "options", "opt", String.class);
}
 

实例 9


public Document serializeDatabaseConnection( DatabaseConnection databaseConnection ) {
  XStream xstream = new XStream(new DomDriver());
  xstream.alias("databaseConnection", DatabaseConnection.class); //$NON-NLS-1$
  xstream.alias("accessType", DatabaseAccessType.class); //$NON-NLS-1$
  String xml = xstream.toXML(databaseConnection);
  System.out.println(xml);
  return null;
}
 

实例 10


private ConfigPersister() {
        _xstream = new XStream(new DomDriver());
        _xstream.alias("launch4jConfig", Config.class);
        _xstream.alias("classPath", ClassPath.class);
        _xstream.alias("jre", Jre.class);
        _xstream.alias("splash", Splash.class);
        _xstream.alias("versionInfo", VersionInfo.class);
        _xstream.addImplicitCollection(Config.class, "headerObjects", "obj",
                        String.class);
        _xstream.addImplicitCollection(Config.class, "libs", "lib", String.class);
        _xstream.addImplicitCollection(Config.class, "variables", "var", String.class);
        _xstream.addImplicitCollection(ClassPath.class, "paths", "cp", String.class);
        _xstream.addImplicitCollection(Jre.class, "options", "opt", String.class);
}
 

实例 11


/**
 * ???XStream???String??????CDATA??"<![CDATA["??????"]]>"? ??XStream???????
 *
 * @param isAddCDATA ????CDATA??
 * @return
 */
public static XStream createXStream(boolean isAddCDATA) {
    XStream xstream = null;
    if (isAddCDATA) {
        xstream = new XStream(new WeChatDomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
    } else {
        xstream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
    }
    return xstream;
}
 

实例 12


private void init() {
        // Create new XStream instance with DomDriver parser
        stream = new XStream(new DomDriver());
        // Register the XML Converters
        stream.registerConverter(new DeviceConverter());
        stream.registerConverter(new TramConverter());
        stream.registerConverter(new DeviceItemConverter());
        // Create alias for V8 XML markup
        stream.alias("char", Device.class);
        stream.alias("deviceItem", DeviceItem.class);
        stream.alias("trame", Trame.class);
        stream.alias("unit", String.class);
        stream.alias("bitsize", Integer.class);
        stream.alias("minvalue", Integer.class);
        stream.alias("maxvalue", Integer.class);
        stream.alias("resolution", Integer.class);
        stream.alias("factor", Double.class);
        stream.alias("offset", Integer.class);
        stream.alias("numbits", Integer.class);
        stream.alias("signed", Boolean.class);
        stream.alias("isFloat", Boolean.class);
}
 

实例 13


private ConfigPersister() {
        _xstream = new XStream(new DomDriver());
        _xstream.alias("launch4jConfig", Config.class);
        _xstream.alias("classPath", ClassPath.class);
        _xstream.alias("jre", Jre.class);
        _xstream.alias("splash", Splash.class);
        _xstream.alias("versionInfo", VersionInfo.class);
        _xstream.addImplicitCollection(Config.class, "headerObjects", "obj",
                        String.class);
        _xstream.addImplicitCollection(Config.class, "libs", "lib", String.class);
        _xstream.addImplicitCollection(Config.class, "variables", "var", String.class);
        _xstream.addImplicitCollection(ClassPath.class, "paths", "cp", String.class);
        _xstream.addImplicitCollection(Jre.class, "options", "opt", String.class);
}
 

实例 14


/**
 * Create XStream for query and report load/save.
 * 
 * @return XStream
 */
 public static XStream createXStream() {
    XStream xstream = new XStream(new DomDriver("UTF-8"));
    xstream.setMode(XStream.NO_REFERENCES);
    xstream.registerConverter(new FontConverter());
    xstream.alias("report", Report.class);
    xstream.alias("chart", Chart.class);
    xstream.useAttributeFor(Report.class, "version");
    xstream.useAttributeFor(Chart.class, "version");
    xstream = createQueryXStream(xstream);
    xstream = createReportXStream(xstream);
    xstream = createChartXStream(xstream);
    return xstream;
}
 

实例 15


private ConfigPersister() {
        _xstream = new XStream(new DomDriver());
        _xstream.alias("launch4jConfig", Config.class);
        _xstream.alias("classPath", ClassPath.class);
        _xstream.alias("jre", Jre.class);
        _xstream.alias("splash", Splash.class);
        _xstream.alias("versionInfo", VersionInfo.class);
        _xstream.addImplicitCollection(Config.class, "headerObjects", "obj",
                        String.class);
        _xstream.addImplicitCollection(Config.class, "libs", "lib", String.class);
        _xstream.addImplicitCollection(Config.class, "variables", "var", String.class);
        _xstream.addImplicitCollection(ClassPath.class, "paths", "cp", String.class);
        _xstream.addImplicitCollection(Jre.class, "options", "opt", String.class);
}
 
讨论
淘淘あ西西 profile image