Staxmate API 的缩进问题

发布于 2024-09-09 23:52:29 字数 1750 浏览 1 评论 0原文

我正在使用 Staxmate API 生成 XML 文件。阅读教程后: http://staxmate.codehaus.org/Tutorial 我尝试进行更改我的代码。最后我添加了调用

doc.setIndentation("\n  ", 1, 1);

,这会导致新生成的 XML 文件为空!如果没有此方法调用,整个 XML 文件就会按预期生成。

怀疑项目设置中存在可疑之处,我使用教程中给出的代码在同一个包中创建了一个测试类:

package ch.synlogic.iaf.export;

import java.io.File;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;

import org.codehaus.staxmate.SMOutputFactory;
import org.codehaus.staxmate.out.SMOutputDocument;
import org.codehaus.staxmate.out.SMOutputElement;

public class Test {

public static void main(String[] args) {
 main("c:\\tmp\\empl.xml");
}

public static void main(String fname)
{
 // 1: need output factory
 SMOutputFactory outf = new SMOutputFactory(XMLOutputFactory.newInstance());
 SMOutputDocument doc;
 try {
  doc = outf.createOutputDocument(new File(fname));

 // (optional) 3: enable indentation (note spaces after backslash!)
 doc.setIndentation("\n  ", 1, 1);
 // 4. comment regarding generation time
 doc.addComment(" generated: "+new java.util.Date().toString());
 SMOutputElement empl = doc.addElement("employee");
 empl.addAttribute(/*namespace*/ null, "id", 123);
 SMOutputElement name = empl.addElement("name");
 name.addElement("first").addCharacters("Tatu");
 name.addElement("last").addCharacters("Saloranta");
 // 10. close the document to close elements, flush output
 doc.closeRoot();
 } catch (XMLStreamException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
}
}

现在,当我从代码中调用 main(String) 方法时,问题仍然存在,而如果我只是运行类测试,因为它工作顺利!我的代码涉及数据库初始化和一些其他产品特定操作。

我迷失了,有什么想法我应该如何继续吗?

I am using Staxmate API to generate XML file. After reading the tutorial: http://staxmate.codehaus.org/Tutorial I tried making the changes in my code. At last I added the call

doc.setIndentation("\n  ", 1, 1);

Which causes the newly generated XML file to be empty! Without this method call entire XML file gets generated as expected.

Suspecting something fishy in in project setup, I created a Test class in the same package with the code given in tutorial:

package ch.synlogic.iaf.export;

import java.io.File;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;

import org.codehaus.staxmate.SMOutputFactory;
import org.codehaus.staxmate.out.SMOutputDocument;
import org.codehaus.staxmate.out.SMOutputElement;

public class Test {

public static void main(String[] args) {
 main("c:\\tmp\\empl.xml");
}

public static void main(String fname)
{
 // 1: need output factory
 SMOutputFactory outf = new SMOutputFactory(XMLOutputFactory.newInstance());
 SMOutputDocument doc;
 try {
  doc = outf.createOutputDocument(new File(fname));

 // (optional) 3: enable indentation (note spaces after backslash!)
 doc.setIndentation("\n  ", 1, 1);
 // 4. comment regarding generation time
 doc.addComment(" generated: "+new java.util.Date().toString());
 SMOutputElement empl = doc.addElement("employee");
 empl.addAttribute(/*namespace*/ null, "id", 123);
 SMOutputElement name = empl.addElement("name");
 name.addElement("first").addCharacters("Tatu");
 name.addElement("last").addCharacters("Saloranta");
 // 10. close the document to close elements, flush output
 doc.closeRoot();
 } catch (XMLStreamException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
}
}

Now when I invoke the main(String) method from my code the problem still persists whereas if I just run class Test as it is it works smoothly! My code involves database initializations and some other product specific actions.

I am lost, any thoughts on how should I proceed with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏月而来 2024-09-16 23:52:29

缩进适用于 Woodstox API

WstxOutputFactory factory = new WstxOutputFactory();
factory.setProperty(WstxOutputFactory.P_AUTOMATIC_EMPTY_ELEMENTS, true);
SMOutputFactory outf = new SMOutputFactory(factory);
doc = outf.createOutputDocument(fout);
doc.setIndentation("\n  ", 1, 1);

Indentation works with Woodstox API

WstxOutputFactory factory = new WstxOutputFactory();
factory.setProperty(WstxOutputFactory.P_AUTOMATIC_EMPTY_ELEMENTS, true);
SMOutputFactory outf = new SMOutputFactory(factory);
doc = outf.createOutputDocument(fout);
doc.setIndentation("\n  ", 1, 1);
痴骨ら 2024-09-16 23:52:29

下面对我有用 -
context.setIndentation("\r\n\t\t\t\t\t\t\t\t", 2, 1); // 按 windows lf 缩进并每级 1 个制表符

Below works for me -
context.setIndentation("\r\n\t\t\t\t\t\t\t\t", 2, 1); // indent by windows lf and 1 tab per level

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文