我正在将JAXB bean编组(序列化)为输出流.如何添加DOCTYPE声明和xml处理指令以进行输出?
我正在做这样的编组:
JAXBContext jaxbContext = JAXBContext.newInstance("com.example.package");
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = schemaFactory.newSchema(schemaSource);
marshaller.setSchema(schema);
marshaller.marshal(object, output);
Run Code Online (Sandbox Code Playgroud)
我希望输出看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Something SYSTEM "some.dtd">
<?xml-stylesheet type="text/xsl" href="some.xsl"?>
Run Code Online (Sandbox Code Playgroud)
JAXB bean是生成的代码,所以我不想更改它们.
有一些hacks和未记录的技巧(请参阅使JAXB生成XML处理指令)来添加xml处理指令和doctype.但是这样做的首选或正确方法是什么?
我正在学习Rust,因为我试图找到一个替代C/C++的互操作C#.
怎么可能像下面的C代码一样编写Rust代码?到目前为止,这是我的Rust代码,没有选择来编组它:
pub struct PackChar { id: u32, val_str: String, }
#[no_mangle]
pub extern fn get_packs_char(size: u32) -> Vec<PackChar> {
let mut out_vec = Vec::new();
for i in 0 .. size {
let int_0 = '0' as u32;
let last_char_val = int_0 + i % (126 - int_0);
let last_char = char::from_u32(last_char_val).unwrap();
let buffer = format!("abcdefgHi{}", last_char);
let pack_char = PackChar {
id: i,
val_str: buffer,
};
out_vec.push(pack_char);
}
out_vec
}
Run Code Online (Sandbox Code Playgroud)
上面的代码试图重现以下C代码,我能够按原样与之互操作.
void GetPacksChar(int size, PackChar** DpArrPnt)
{
int TmpStrSize …Run Code Online (Sandbox Code Playgroud) 我有以下.NET值类型:
[StructLayout(LayoutKind.Sequential)]
public struct Date
{
public UInt16 V;
}
[StructLayout(LayoutKind.Sequential)]
public struct StringPair
{
public String A;
public String B;
public String C;
public Date D;
public double V;
}
Run Code Online (Sandbox Code Playgroud)
我有代码将指向值类型的指针传递给非托管代码,以及通过调用System.Runtime.InteropServices.Marshal.OffsetOf发现的偏移量.非托管代码填充Date和double值.
为StringPair结构报告的偏移量正是我所期望的:0,8,16,24,32
我在测试函数中有以下代码:
FieldInfo[] fields = typeof(StringPair).GetFields(BindingFlags.Instance|BindingFlags.Public);
for ( int i = 0; i < fields.Length; i++ )
{
int offset = System.Runtime.InteropServices.Marshal.OffsetOf(typeof(StringPair), fields[i].Name).ToInt32();
Console.WriteLine(String.Format(" >> field {0} @ offset {1}", fields[i].Name, offset));
}
Run Code Online (Sandbox Code Playgroud)
这打印出这些偏移.
>> field A @ offset 0
>> field B @ offset 8
>> field …Run Code Online (Sandbox Code Playgroud) 我想知道是否有可能让JAXB不为用作包装器的XML元素创建Java对象.例如,对于以下结构的XML
<root>
<wrapper>
<entity/>
</wrapper>
</root>
Run Code Online (Sandbox Code Playgroud)
我根本不想要<wrapper>创建一个对象.所以对于像这样的课程
class Root {
private Entity entity;
}
Run Code Online (Sandbox Code Playgroud)
该<entity>元件应直接解组到实体字段.
是否可以使用JAXB实现?
我在不使用XSD的情况下使用Jaxb 2.0 api,并使用注释创建了内容模型.我想为进行编组的类编写一个Junit测试.我最初的计划是将预期的XML String与用于断言的实际XML String进行比较(最明显的选择).但是我发现编组创建了xml,其中属性/属性顺序是不可预测的(实际上我不知道默认顺序是什么).现在,如果是这种情况,我不能假设一个预定义的xml字符串,然后将其与编组的字符串进行比较.我想要断言marshaller类的另一种方法如下:
1 - 创建内容模型.
2马歇尔吧.
3-Unmarshall在步骤2中创建的xml以获取模型.
4-Do断言基于第1步和第3步的模型,用于属性/属性.
但我仍然觉得这不令人满意.在这种情况下为编组编写Junit测试的正确方法是什么?
虽然使用编组的xml的实际应用程序不依赖于xml属性/属性顺序,但是Junit测试似乎很棘手.
谢谢
一个项目需要结合使用以下杰克逊注释的组合.那么,有没有办法创建另一个注释,以避免丑陋的复制/粘贴:
public class A {
@JsonProperty("_id")
@JsonSerialize(using=IdSerializer.class)
@JsonDeserialize(using=IdDeserializer.class)
String id;
}
public class B {
@JsonProperty("_id")
@JsonSerialize(using=IdSerializer.class)
@JsonDeserialize(using=IdDeserializer.class)
String id;
}
public class C {
@CustomId // don't repeat that configuration endlessly
String id;
}
Run Code Online (Sandbox Code Playgroud)
更新:我试过这个,没有成功:-(
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonProperty("_id")
@JsonSerialize(using=IdSerializer.class, include=JsonSerialize.Inclusion.NON_NULL)
@JsonDeserialize(using=IdDeserializer.class)
public @interface Id {}
public class D {
@Id
private String id;
}
Run Code Online (Sandbox Code Playgroud) 问题是如何生成XML文件输出而不是system.out?
package jaxbintroduction;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
itemorder.Book quickXML = new itemorder.Book();
quickXML.setAuthor("Sillyme");
quickXML.setDescription("Dummie book");
quickXML.setISBN(123456789);
quickXML.setPrice((float)12.6);
quickXML.setPublisher("Progress");
quickXML.setTitle("Hello World JAVA");
try {
javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(quickXML.getClass().getPackage().getName());
javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); //NOI18N
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(quickXML, System.out);
OutputStream os = new FileOutputStream( "nosferatu.xml" );
marshaller.marshal( quickXML, os );
} catch (javax.xml.bind.JAXBException ex) {
// …Run Code Online (Sandbox Code Playgroud) 当作者来创建文档的根时,作者开始如下:
//This statement means that class "Bookstore.java" is the root-element of our example
@XmlRootElement(namespace = "de.vogella.xml.jaxb.model")
public class Bookstore {
...
}
Run Code Online (Sandbox Code Playgroud)
虽然我将手动生成我的类而不是让Eclipse这样做,但我将提供一个XSD带有我的jar文件(不在内部打包,而是在包含jar文件的文件夹中),这样当我的应用程序启动时,它将验证XML文档是否已被篡改.
那么,在XSD文件中,targetNamespace是de.vogella.xml.jaxb.model 因为它在上面声明为@XmlRootElement(namespace = "de.vogella.xml.jaxb.model")?
如何将嵌套结构编组为JSON?我知道如何在没有任何嵌套结构的情况下编组结构.但是,当我尝试使JSON响应看起来像这样:
{"genre": {"country": "taylor swift", "rock": "aimee"}}
Run Code Online (Sandbox Code Playgroud)
我遇到了问题.
我的代码看起来像这样:
走:
type Music struct {
Genre struct {
Country string
Rock string
}
}
resp := Music{
Genre: { // error on this line.
Country: "Taylor Swift",
Rock: "Aimee",
},
}
js, _ := json.Marshal(resp)
w.Write(js)
Run Code Online (Sandbox Code Playgroud)
但是,我得到了错误
Missing type in composite literal
我该如何解决这个问题?
我想从非托管C++传递大约100 - 10,000点到C#.
C++端看起来像这样:
__declspec(dllexport) void detect_targets( char * , int , /* More arguments */ )
{
std::vector<double> id_x_y_z;
// Now what's the best way to pass this vector to C#
}
Run Code Online (Sandbox Code Playgroud)
现在我的C#端看起来像这样:
using System;
using System.Runtime.InteropServices;
class HelloCpp
{
[DllImport("detector.dll")]
public static unsafe extern void detect_targets( string fn , /* More arguments */ );
static void Main()
{
detect_targets("test.png" , /* More arguments */ );
}
}
Run Code Online (Sandbox Code Playgroud)
我如何改变我的代码,以便将std :: vector从非托管C++传递给C#?