将Java类序列化为XML的最合适方法是什么?我尝试过JAXB,但它在Interfaces和Generics方面存在问题.哪种解决方案侵入性最小但可扩展?
I'm creating a program that allow user define formulas with on 4 basic operation: add, subtract, divide, multiple using XML. Let's take an example: User want to define formula like (a + b) x (c + d). The format of the xml as following:
EDIT I had implement this
编辑解决.非常感谢Yaniv的建议.我的解决方案如下:
<xPlugins>
<xPlugin>
<Multiple>
<Add>
<Operator>
<value>1</value>
</Operator>
<Operator>
<value>2</value>
</Operator>
</Add>
<Add>
<Operator>
<value>3</value>
</Operator>
<Operator>
<value>4</value>
</Operator>
</Add>
</Multiple>
</xPlugin>
</xPlugins>
Run Code Online (Sandbox Code Playgroud)
类
//root element
public …Run Code Online (Sandbox Code Playgroud) 我试图反序列化一些XML,我无法获得命名空间/ xsi:type="Model"工作.如果xsi:type="Model"它不在XML中,它可以工作,但它必须在那里.如果我将命名空间从模型中删除,我会收到错误,如果我重命名它,我会得到一个空列表.
XML
<Vehicles xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Vehicle xsi:type="Model">
<Id>238614402</Id>
</Vehicle>
<Vehicle xsi:type="Model">
<Id>238614805</Id>
</Vehicle>
</Vehicles>
Run Code Online (Sandbox Code Playgroud)
模型
[XmlRootAttribute("Vehicles")]
public class Vehicles
{
public Vehicles()
{
Vehicle = new List<Vehicle>();
}
[XmlElement(ElementName = "Vehicle", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public List<Vehicle> Vehicle { get; set; }
}
public class Vehicle
{
[XmlElement("Id")]
public int Id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
反序列化
XmlSerializer serializer = new XmlSerializer(typeof(Vehicles));
string carXML = "<Vehicles xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><Vehicle xsi:type=\"Model\"> <Id>238614402</Id> </Vehicle><Vehicle xsi:type=\"Model\"> <Id>238614805</Id> </Vehicle></Vehicles>";
var cars = …Run Code Online (Sandbox Code Playgroud) 我retrofit2.0在我的应用程序中使用simpleframework.xml库.
问题是当我没有proguard运行应用程序时它工作正常但是当我运行proguard时我在日志中得到以下错误.
E/ERROR: java.lang.RuntimeException: org.simpleframework.xml.core.PersistenceException: Constructor not matched for class A
Run Code Online (Sandbox Code Playgroud)
A类没有/ default构造函数应该可以工作.我还添加了一个No Argument Constructor.但这并没有纠正这个问题.
类一
@Root(name = "data",strict = false)
public class A {
@Element(name = "baseurl",required = false)
private String baseURl;
@Element(name = "country_code")
private String country_code;
// Setters and getters
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,没有构造函数(添加默认的空构造函数可以解决问题).所以默认的No Argument Constructor应该也能正常工作.但是,我尝试使用以下构造函数,这将删除错误.
public A(@ELement(name = "baseurl") String baseUrl,
@Element(name = "country_code") String country_code) { // Add all the elements from the xml in the constructor i.e. …Run Code Online (Sandbox Code Playgroud) android proguard xml-deserialization android-proguard simple-xml-converter
我有一些 XML 格式的简单数据,我需要将其转换为 JSON,并且还能够将 JSON 转换回相同的 XML 字符串。但是我在使用现有的 jackson(2.0.6 版)库时遇到了问题。
这是具有相似结构的 XML 数据示例
<channels>
<channel>A</channel>
<channel>B</channel>
<channel>C</channel>
</channels>
Run Code Online (Sandbox Code Playgroud)
为了能够将其转换回原始 XML,我希望 JSON 看起来像这样
{
"channels": {
"channel": [
"A",
"B",
"C"
]
}
}
Run Code Online (Sandbox Code Playgroud)
然而杰克逊给了我
{"channel":"C"}
Run Code Online (Sandbox Code Playgroud)
不保留根元素名称,而是创建通道数组,最后一个覆盖之前的。
查看 com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer 的源代码,我发现该库不支持此功能,但允许覆盖和更改行为。
/**
* Method called when there is a duplicate value for a field.
* By default we don't care, and the last value is used.
* Can be overridden to provide alternate handling, such as throwing
* an exception, or choosing …Run Code Online (Sandbox Code Playgroud) 使用Java将Java Bean 序列化为XML XMLEncoder并且XMLDecoder看起来非常简洁:许多来源的许多类都可以使用它们的公共接口可靠地序列化.甚至在API参考的许多地方也建议使用这种方法进行序列化.但是用于此的XML语法似乎非常强大.是否有任何安全机制可以防止来自恶意文档的攻击?或者,XMLDecoder出于安全原因,是否应避免使用不受信任的文件?
java security xml-serialization javabeans xml-deserialization
当我尝试将XML解析为对象时,我得到一个System.FormatException.据我所知,这是由于System.Xml.Serialization.XmlSerializer.Deserialize中使用的文化,期望一个点作为十进制字符,但xml包含一个逗号.
该对象如下所示:
public sealed class Transaction
{
[XmlElement("transactionDate")]
public DateTime TransactionDate { get; set; }
[XmlElement("transactionAmount")]
public decimal Amount { get; set; }
[XmlElement("transactionDescription")]
public string Description { get; set; }
[XmlElement("transactionType")]
public int Type { get; set; }
public static Transaction FromXmlString(string xmlString)
{
var reader = new StringReader(xmlString);
var serializer = new XmlSerializer(typeof(Transaction));
var instance = (Transaction) serializer.Deserialize(reader);
return instance;
}
}
xml:
<transaction>
<transactionDate> 2013-07-02 <transactionDate>
<transactionAmount>-459,00</transactionAmount>
<transactionDescription>description</transactionDescription>
<transactionType>1</transactionType>
</transaction>
我通过引入第二个使用我自己的文化解析第一个属性的方法来实现它:
namespace MyNamespace
{
[XmlRoot("transaction"), XmlType("Transaction")]
public …Run Code Online (Sandbox Code Playgroud) 我需要从XML中提取商品,但要考虑节点顺序:
<items>
<offer/>
<product>
<offer/>
<offer/>
</product>
<offer/>
<offer/>
</items>
以下结构将解码值,但会解码为两个不同的切片,这将导致原始顺序丢失:
type Offers struct {
Offers []offer `xml:"items>offer"`
Products []offer `xml:"items>product>offer"`
}
有任何想法吗?
我有一个XML文档,我需要将其转换(反序列化)到Java POJO中.我无法更改 XML文档的结构.我使用Java 8和Jackson框架进行映射. Gradle依赖项:
dependencies {
compile('com.fasterxml.jackson.dataformat:jackson-dataformat-xml')
compile('org.springframework.boot:spring-boot-starter-freemarker')
compile('org.springframework.boot:spring-boot-starter-web')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Run Code Online (Sandbox Code Playgroud)
包装XML文档:
@JacksonXmlRootElement(localName = "rates_response")
public class RatesResponse implements Serializable{
private static final long serialVersionUID = 3254688495454519L;
@JacksonXmlProperty(isAttribute = true)
private String b_number = "";
@JacksonXmlProperty(isAttribute = true)
private String l_premium = "";
@JacksonXmlProperty(isAttribute = true)
private String currency = "";
@XmlElement(required = false)
@JacksonXmlProperty
private String c_b_relationship = null;
@JacksonXmlProperty(isAttribute = true)
private String n_of_p_loans = null;
/*
* Key: status_code
* Key: …Run Code Online (Sandbox Code Playgroud) xml jackson xml-deserialization java-8 jackson-dataformat-xml
使用 Import-CliXml 命令重新导入反序列化对象时,我遇到了 Powershell 5 类和对象类型的问题。
我有一个 Computer 类型的对象,我希望将其存储为 xml,然后在下次运行脚本时重新导入
class Computer
{
$Private:hostname
$Private:ipAddress
Computer([String] $hostname, [String] $ipAddress)
{
$this.hostname = $hostname
$this.ipAddress = $ipAddress
}
static [Computer] reserialize([PSObject] $deserializedComputer)
{
return [Computer]::new($deserializedComputer.hostname, $deserializedComputer.ipAddress)
}
}
Run Code Online (Sandbox Code Playgroud)
我使用以下方法导出和导入对象:
$computer = [Computer]::new("test-machine", "192.168.1.2")
$computer | Export-CliXml C:\Powershell\exportTest.xml
$deserializedComputer = Import-CliXml C:\Powershell\exportTest.xml
Run Code Online (Sandbox Code Playgroud)
我知道当这个对象被重新导入时,它被反序列化并且基本上只是一个数据容器([Deserialized.Computer] 类型)。在尝试使用我的重新序列化方法重新序列化它之前,我试图弄清楚如何对这个对象进行类型检查。
例如,如果我尝试投射 $deserializedComputer 它告诉我:
Cannot convert value "Computer" to type "Computer". Error: "Cannot convert the "Computer" value of type "Deserialized.Computer" to type
"Computer"."
Run Code Online (Sandbox Code Playgroud)
我明白为什么不能强制转换,我只是使用错误消息指出该对象知道它的类型为 [Deserialized.Computer]
我找不到从 $deserializedComputer.getMember() 返回的任何内容表明它是 [Deserialized.Computer] …
powershell serialization xml-deserialization deserialization powershell-5.0