com.thoughtworks.xstream.XStream.fromXML()方法的使用及代码示例

x33g5p2x  于2022-02-02 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(308)

本文整理了Java中com.thoughtworks.xstream.XStream.fromXML()方法的一些代码示例,展示了XStream.fromXML()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。XStream.fromXML()方法的具体详情如下:
包路径:com.thoughtworks.xstream.XStream
类名称:XStream
方法名:fromXML

XStream.fromXML介绍

[英]Deserialize an object from a file. Depending on the parser implementation, some might take the file path as SystemId to resolve additional references.
[中]从文件中反序列化对象。根据解析器实现的不同,有些可能会将文件路径作为SystemId来解析其他引用。

代码示例

代码示例来源:origin: jenkinsci/jenkins

/**
 * Returns a list of any plugins that are persisted in the installing list
 */
@SuppressWarnings("unchecked")
public static synchronized @CheckForNull Map<String,String> getPersistedInstallStatus() {
  File installingPluginsFile = getInstallingPluginsFile();
  if(installingPluginsFile == null || !installingPluginsFile.exists()) {
  return null;
  }
  return (Map<String,String>)new XStream().fromXML(installingPluginsFile);
}

代码示例来源:origin: stackoverflow.com

map.put("island","faranga");
XStream magicApi = new XStream();
magicApi.registerConverter(new MapEntryConverter());
magicApi.alias("root", Map.class);
String xml = magicApi.toXML(map);
System.out.println("Result of tweaked XStream toXml()");
System.out.println(xml);
Map<String, String> extractedMap = (Map<String, String>) magicApi.fromXML(xml);
assert extractedMap.get("name").equals("chris");
assert extractedMap.get("island").equals("faranga");

代码示例来源:origin: geoserver/geoserver

XStreamPersister persister = XSTREAM_PERSISTER_FACTORY.createXMLPersister();
XStream xs = persister.getXStream();
String xml = xs.toXML(source);
T copy = (T) xs.fromXML(xml);
return copy;

代码示例来源:origin: apache/ignite

/**
 * Reads configuration from given file and delete the file after.
 *
 * @param fileName File name.
 * @return Readed configuration.
 * @throws IOException If failed.
 * @see #storeToFile(IgniteConfiguration, boolean)
 * @throws IgniteCheckedException On error.
 */
private static IgniteConfiguration readCfgFromFileAndDeleteFile(String fileName)
  throws IOException, IgniteCheckedException {
  try(BufferedReader cfgReader = new BufferedReader(new FileReader(fileName))) {
    IgniteConfiguration cfg = (IgniteConfiguration)new XStream().fromXML(cfgReader);
    if (cfg.getMarshaller() == null) {
      Marshaller marsh = IgniteTestResources.getMarshaller();
      cfg.setMarshaller(marsh);
    }
    X.println("Configured marshaller class: " + cfg.getMarshaller().getClass().getName());
    if (cfg.getDiscoverySpi() == null) {
      TcpDiscoverySpi disco = new TcpDiscoverySpi();
      disco.setIpFinder(GridCacheAbstractFullApiSelfTest.LOCAL_IP_FINDER);
      cfg.setDiscoverySpi(disco);
    }
    X.println("Configured discovery: " + cfg.getDiscoverySpi().getClass().getName());
    return cfg;
  }
  finally {
    new File(fileName).delete();
  }
}

代码示例来源:origin: kiegroup/optaplanner

public static <T> T serializeAndDeserializeWithXStream(T input) {
  XStream xStream = new XStream();
  xStream.setMode(XStream.ID_REFERENCES);
  if (input != null) {
    xStream.processAnnotations(input.getClass());
  }
  XStream.setupDefaultSecurity(xStream);
  xStream.addPermission(new AnyTypePermission());
  String xmlString = xStream.toXML(input);
  return (T) xStream.fromXML(xmlString);
}

代码示例来源:origin: Netflix/eureka

@Test
public void testEncodingDecodingWithoutMetaData() throws Exception {
  Applications applications = InstanceInfoGenerator.newBuilder(10, 2).withMetaData(false).build().toApplications();
  XStream xstream = JsonXStream.getInstance();
  String jsonDocument = xstream.toXML(applications);
  Applications decodedApplications = (Applications) xstream.fromXML(jsonDocument);
  assertThat(EurekaEntityComparators.equal(decodedApplications, applications), is(true));
}

代码示例来源:origin: stackoverflow.com

XStream xStream = new XStream();
xStream.alias("Strings", String[].class);
xStream.alias("String", String.class);
String[] result = (String[])xStream.fromXML(file);

代码示例来源:origin: kiegroup/optaplanner

protected <S extends Score, W extends TestScoreWrapper<S>> void assertSerializeAndDeserialize(S expectedScore, W input) {
  XStream xStream = new XStream();
  xStream.setMode(XStream.ID_REFERENCES);
  xStream.processAnnotations(input.getClass());
  XStream.setupDefaultSecurity(xStream);
  xStream.allowTypesByRegExp(new String[]{"org\\.optaplanner\\.\\w+\\.config\\..*",
      "org\\.optaplanner\\.persistence\\.xstream\\..*\\$Test\\w+ScoreWrapper"});
  String xmlString = xStream.toXML(input);
  W output = (W) xStream.fromXML(xmlString);
  assertEquals(expectedScore, output.getScore());
  String regex;
  if (expectedScore != null) {
    regex = "<([\\w\\-\\.]+)( id=\"\\d+\")?>" // Start of element
        + "\\s*<score( id=\"\\d+\")?>"
        + expectedScore.toString().replaceAll("\\[", "\\\\[").replaceAll("\\]", "\\\\]") // Score
        + "</score>"
        + "\\s*</\\1>"; // End of element
  } else {
    regex = "<([\\w\\-\\.]+)( id=\"\\d+\")?/>"; // Start and end of element
  }
  if (!xmlString.matches(regex)) {
    fail("Regular expression match failed.\nExpected regular expression: " + regex + "\nActual string: " + xmlString);
  }
}

代码示例来源:origin: Netflix/eureka

@Test
public void testEncodingDecodingWithMetaData() throws Exception {
  Applications applications = InstanceInfoGenerator.newBuilder(10, 2).withMetaData(true).build().toApplications();
  XStream xstream = JsonXStream.getInstance();
  String jsonDocument = xstream.toXML(applications);
  Applications decodedApplications = (Applications) xstream.fromXML(jsonDocument);
  assertThat(EurekaEntityComparators.equal(decodedApplications, applications), is(true));
}

代码示例来源:origin: stackoverflow.com

XStream xstream = new XStream();
xstream.alias("comments", Comments.class);
xstream.alias("comment", Comment.class);
xstream.addImplicitCollection(Comments.class, "comments");
Comments comments = (Comments)xstream.fromXML(xml);

代码示例来源:origin: stackoverflow.com

import com.thoughtworks.xstream.XStream;

public class deepCopy {
  private static  XStream xstream = new XStream();

  //serialize with Xstream them deserialize ...
  public static Object deepCopy(Object obj){
    return xstream.fromXML(xstream.toXML(obj));
  }
}

代码示例来源:origin: Netflix/eureka

@Test
public void testEncodingDecodingWithoutMetaData() throws Exception {
  Applications applications = InstanceInfoGenerator.newBuilder(10, 2).withMetaData(false).build().toApplications();
  XStream xstream = XmlXStream.getInstance();
  String xmlDocument = xstream.toXML(applications);
  Applications decodedApplications = (Applications) xstream.fromXML(xmlDocument);
  assertThat(EurekaEntityComparators.equal(decodedApplications, applications), is(true));
}

代码示例来源:origin: stackoverflow.com

XStream xStream = new XStream();
xStream.alias("Strings", ArrayList.class);
xStream.alias("String", String.class);
xStream.addImplicitArray(ArrayList.class, "elementData");
List <String> result = (List <String>)xStream.fromXML(file);

代码示例来源:origin: stackoverflow.com

XStream xstream = new XStream();
  //converting object to XML
  String xml = xstream.toXML(myObject);
  //converting xml to object
  MyClass myObject = (MyClass)xstream.fromXML(xml);

代码示例来源:origin: Netflix/eureka

@Test
public void testEncodingDecodingWithMetaData() throws Exception {
  Applications applications = InstanceInfoGenerator.newBuilder(10, 2).withMetaData(true).build().toApplications();
  XStream xstream = XmlXStream.getInstance();
  String xmlDocument = xstream.toXML(applications);
  Applications decodedApplications = (Applications) xstream.fromXML(xmlDocument);
  assertThat(EurekaEntityComparators.equal(decodedApplications, applications), is(true));
}

代码示例来源:origin: psi-probe/psi-probe

try {
 try (InputStream fis = Files.newInputStream(file.toPath())) {
  stats = (Map<String, List<XYDataItem>>) (new XStream().fromXML(fis));

代码示例来源:origin: stackoverflow.com

XStream xstream = new XStream();
// marshalling
String xml = xstream.toXML(domainObject);
// unmarshalling
domainObject = xstream.fromXML(xml);

代码示例来源:origin: jenkinsci/jenkins

String xml = Jenkins.XSTREAM.toXML(src);
Node result = (Node) Jenkins.XSTREAM.fromXML(xml);
result.setNodeName(name);
if(result instanceof Slave){ //change userId too

代码示例来源:origin: mbechler/marshalsec

/**
 * {@inheritDoc}
 *
 * @see marshalsec.MarshallerBase#unmarshal(java.lang.Object)
 */
@Override
public Object unmarshal ( String data ) throws Exception {
  com.thoughtworks.xstream.XStream xs = new com.thoughtworks.xstream.XStream();
  return xs.fromXML(data);
}

代码示例来源:origin: stackoverflow.com

public static <T> T cloneObject(
 T object) {
 XStream xstream = new XStream();
 return (T) xstream.fromXML(xstream.toXML(object));
}

相关文章