教程列表
开源项目
知识点
相关文章
更多最近更新
更多XStream类混叠alias创建别名
2019-04-27 16:19|来源: 网路
类混叠是用来创建一个类的XML完全限定名称的别名。让我们修改原来的例子,下面的代码添加到它。
xstream.alias("student", Student.class); xstream.alias("note", Note.class);
让我们使用 XStream 测试上述对象序列化。
创建一个Java类名为XStreamTester在文件 C:\>XStream_WORKSPACE\com656463\xstream.
文件: XStreamTester.java
package com656463.xstream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.List; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.stream.StreamResult; import org.xml.sax.InputSource; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.StaxDriver; public class XStreamTester { public static void main(String args[]){ XStreamTester tester = new XStreamTester(); XStream xstream = new XStream(new StaxDriver()); xstream.alias("student", Student.class); xstream.alias("note", Note.class); Student student = tester.getStudentDetails(); //Object to XML Conversion String xml = xstream.toXML(student); System.out.println(formatXml(xml)); } private Student getStudentDetails(){ Student student = new Student("Mahesh"); student.addNote(new Note("first","My first assignment.")); student.addNote(new Note("second","My Second assignment.")); return student; } public static String formatXml(String xml){ try{ Transformer serializer= SAXTransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source xmlSource=new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes()))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); serializer.transform(xmlSource, res); return new String(((ByteArrayOutputStream)res.getOutputStream()).toByteArray()); }catch(Exception e){ return xml; } } } class Student { private String studentName; private List<Note> notes = new ArrayList<Note>(); public Student(String name) { this.studentName = name; } public void addNote(Note note) { notes.add(note); } public String getName(){ return studentName; } public List<Note> getNotes(){ return notes; } } class Note { private String title; private String description; public Note(String title, String description) { this.title = title; this.description = description; } public String getTitle(){ return title; } public String getDescription(){ return description; } }
验证结果
使用javac编译器编译如下类:
C:\XStream_WORKSPACE\com656463\xstream>javac XStreamTester.java
现在运行XStreamTester看到的结果:
C:\XStream_WORKSPACE\com656463\xstream>java XStreamTester
验证输出
<?xml version="1.0" encoding="UTF-8"?> <student> <studentName>Mahesh</studentName> <notes> <note> <title>first</title> <description>My first assignment.</description> </note> <note> <title>second</title> <description>My Second assignment.</description> </note> </notes> </student>
相关问答
更多-
假设你有一个类型属性的Coin类,如下所示: @XStreamAlias("coin") public class Coin { String type; } 而且你有一个Coins类来强制Coin列表: @XStreamAlias("coins") public class Coins{ @XStreamImplicit List
coins = new ArrayList (); } 注意注释。 列表是隐含的,硬币类将显示为“硬币”。 输出将是: 创建一个别名,然后存储?(creating an alias and then store?)[2022-09-11]
是的,只需将控制台中的确切alias命令复制到~/.basrc文件中即可。 请记住,重新启动终端仿真器后,新别名将生效(严格来说, bash过程)。 编辑: 关于该主题的高评级SO问题的强制性链接 。 Yes, just copy the exact alias command like it was in the console, into the ~/.basrc file. Remember that the new alias will take effect after you relaunch ...如果您尝试用X和Y坐标绘制线条,则坐标将定义线条的中心。 所以我假设你定义了这样的行: myLine.StrokeThickness = 1; myLine.Y1 = 10; myLine.Y2 = 10; myLine.X1 = 0; myLine.X2 = 50; 这导致线的中心在两个像素之间(从顶部的像素9和10)。 所以渲染总是发生在相邻的像素中(因此,至少在理论上,导致每个奇怪的StrokeThickness模糊/更宽的线)。 要解决此问题,请尝试将StrokeThickness / 2添加到Y ...XStream Alias与先前文件内容结构的兼容性(XStream Alias Compatibility With Previous Files Content Structure)[2022-07-16]
设置别名以匹配您的旧类名,您可以将您的类移动到您想要的任何包。 XStream xstream = new XStream(); xstream.alias("myproject.data.clientconfiguration.InstallationUser", InstallationUser.class); Set alias to match your old class name, and you can move your class to any package you want. XSt ...XStream短动态别名(XStream short dynamic aliases)[2022-05-06]
在内部,XStream使用其Mapper接口来处理类和字段到XML中相应名称的映射。 这个接口有大量的实现。 XStream类本身可以在其构造函数中使用Mapper 。 您可能想要查看该类的源代码以查看默认情况下使用哪个Mapper实现,然后编写自己的实现,以便自动为您执行别名。 例如, ClassAliasingMapper看起来很有用。 Internally, XStream uses its Mapper interface to handle the mapping of classes and f ...你不能,但我总是认为类别名无论如何都是一个愚蠢的想法。 如果你真的需要别名一个类,你可以欺骗并使用继承。 class Foo { public static function printname() { echo get_called_class(); } } class Bar extends Foo { } Bar::printname(); // Print "Bar" You can't, but I always thought class aliases ...bash别名无法创建(bash alias not able to create)[2020-01-27]
您需要在等号后删除空格。 You need to remove the space after the equal sign.我通过改变Point类来解决它; public class Point { /* @XStreamAlias("real") @XStreamAlias("real2") public Real y = new Real("y", "21"); @XStreamAsAttribute public String id = "position"; */ @XStreamImplicit public List xy = ...你可以使用doskey : doskey ct=cleartool $* 您可以将该想法概括为其他ClearCase命令: doskey ct=cleartool $* doskey mt=multitool $* doskey credmap="C:\Program Files\Rational\ClearCase\etc\utils\credmap.exe" $* doskey creds="C:\Program Files\Rational\ClearCase\etc\utils\creds.exe ...我不相信你可以用简单的别名来做到这一点。 但是您可以使用下面的自定义序列化程序来完成它。 xstream doc有一个很好的转换器教程 ,其中包含有关如何注册自定义转换器的详细信息。 public class Nodo { String id; ArrayListchildren = new ArrayList<>(); } public class NodoConverter implements Converter { /** {@inheritDoc} */ @Ov ...