简体   繁体   English

Java-stAX-从字符串创建.xml

[英]Java - stAX - Creating .xml from String

I need to create a singular, and potentially very large .xml file and my research has made me aware of stAX. 我需要创建一个单一的,可能非常大的.xml文件,而我的研究使我意识到了stAX。 I've spent a while trying to get my head around it, but I'm not really sure if its really the right tool for the job. 我花了一些时间试图解决这个问题,但是我不确定它是否真的是正确的工具。

So basicly, I've got a single String, this string contains an formatted XML message. 因此,基本上,我只有一个String,此字符串包含格式化的XML消息。 So from this message I need to be able to write this as a well formed .xml file. 因此,根据此消息,我需要能够将其编写为格式良好的.xml文件。 Here is an example XML string: (Not complete, the final String is much more complex) 这是一个XML字符串示例:(不完整,最终的String要复杂得多)

<RISKADDRESS><ASINPUT><HOUSENR/><HOUSENAME/><STREET1>86 Warren Avenue</STREET1><STREET2/><DISTRICT/><CITY>Southampton</CITY><COUNTY>GB</COUNTY><POSTCODE>SO16 6AE</POSTCODE><COUNTRY>GB</COUNTRY></ASINPUT></RISKADDRESS>

Just as a sidenote, I can also get my data as a HashMap. 就像一个旁注一样,我也可以将我的数据作为HashMap获得。

Can anyone point me in the right direction, what are the steps involved in this process? 谁能指出我正确的方向,这个过程涉及哪些步骤? All of the tutorials I've come across so far have not really shed any light on my issue. 到目前为止,我所遇到的所有教程并没有真正阐明我的问题。 Below is an example of where I've got to, before getting stuck. 以下是在陷入困境之前必须前往的示例。

XMLOutputFactory factory = XMLOutputFactory.newInstance ();
XMLStreamWriter writer = new IndentingXMLStreamWriter (factory.createXMLStreamWriter (out));

writer.writeStartDocument ("UTF-8", "1.0");

//Do i have to iterate over my String in some way here?


writer.writeEndDocument ();

writer.flush ();
writer.close ();

Edit: I should add that I ideally need to make this code reusable, so specifying the exact attribute names and so on isn't really an option. 编辑:我应该补充一点,我理想情况下需要使此代码可重用,因此指定确切的属性名称等并不是真正的选择。

I cant create any comment, but did you check out this question? 我无法发表任何评论,但是您检查了这个问题吗? how to parse xml to java object? 如何将xml解析为java对象?

Maybe a duplicate? 也许重复?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM