问题标签 [stax]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
419 浏览

java - Metro & StreamBuffer 命名空间错误 - 我理解正确吗?

总而言之,我(仍在)致力于从 Java/Metro 2.0 调用 WSE 3.0 .NET Web 服务。我们已经设置了安全要求,以便我们加密消息的正文和标头并签署标头。我能够向他们的服务发送请求并获得响应,但 Metro 无法验证签名并从 WSIT 代码中引发错误。我从 CVS 获得了最新的 WSIT 代码,这是该方法的代码片段:

来自 com.sun.xml.ws.security.opt.impl.incoming.GenericSecuredHeader:

如您所见,其中有一个 TODO,我认为这表明该方法无法处理我所遇到的情况,但我不确定我是否知道它们所说的“范围内”命名空间是什么意思。

这是它正在中断的 Web 服务响应中的 XML 片段:

在堆栈跟踪(包括在下面)中,它抱怨无法验证引用 URI="#Id-46282a5d-c7fa-403c-8ac9-f7df0dfdb0cf"。 我不清楚的是 - 它是否因为 Reference 元素没有命名空间属性或前缀而失败? 应该允许他们从父 Signature 元素继承命名空间,还是 StreamBuffer 类不能处理?

相比之下,这是我的请求(由 WSIT 生成)中的一个类似 XML 片段,其中 ds 前缀是

xmlns:ds="http://www.w3.org/2000/09/xmldsig#"

当 Metro 生成这些块时,所有内容都有一个命名空间前缀。

如果你还在读这么远 - 谢谢!以下是堆栈跟踪的一些片段:

0 投票
1 回答
1350 浏览

java - 如何规范化 Stax XML 对象

我想规范化一个 Stax 对象,它使用 DOM 执行的程序,但 dom 无法管理大型 XML 文档(如 1GB),所以 STAX 是解决方案。

我拥有的代码是:

它的想法是用 Stax 做下面的代码......

谢谢 :)

0 投票
4 回答
24272 浏览

java - StAX - 使用 XMLStreamWriter 设置版本和编码

我正在使用 StAX 创建 XML 文件,然后使用 XSD 验证文件。

创建 XML 文件时出现错误:

这是代码片段:

我在 Unix 上运行这段代码。有人知道如何设置版本和编码样式吗?

0 投票
3 回答
12772 浏览

java - 使用 StAX 格式化 XML 文件

我正在使用 StAX XML 流编写器来编写 XML 文件。它将所有数据写入一行。我希望所有标签都缩进而不是单行。

0 投票
7 回答
2834 浏览

java - 如何在 CDATA 之外编写未转义的 XML

我正在尝试使用 Stax 编写 XML 数据,其中内容本身是 HTML

如果我尝试

我明白了

然后我注意到 CDATA 方法并将我的代码更改为:

这次结果是

这仍然不好。我真正想要的是

那么是否有一个 XML API/Library 允许我在不进入 CDATA 部分的情况下编写原始文本?到目前为止,我已经查看了 Stax 和 JDom,但他们似乎没有提供这个。

最后,我可能会求助于好的旧 StringBuilder ,但这并不优雅。

更新:

到目前为止,我大多同意答案。但是,<b>here</b>我可以有一个 1MB 的 HTML 文档,而不是我想嵌入到更大的 XML 文档中。你的建议意味着我必须解析这个 HTML 文档才能理解它的结构。如果可能的话,我想避免这种情况。

回答:

这是不可能的,否则您可能会创建无效的 XML 文档。

0 投票
3 回答
1698 浏览

xml - 独立于物理 XML 文件的单元测试 XML

我的问题是:

在 JUnit 中,如何在不从物理存储在文件系统上的 XML 文件中读取 SUT 的情况下为我的被测系统(SUT)设置 xml 数据

背景

我得到一个 XML 文件,其中包含创建发票的规则。我的工作是将这些规则从 XML 转换为 Java 对象,例如,如果我的 XML 文件中有如下标签,表明在 30 天后,交易无法开具发票

这将转换为 Java 类,例如 ExpirationDateInvoicingRule

我有一个类 InvoiceConfiguration 应该采用 XML 文件并创建 *InvoicingRule 对象。我正在考虑使用 StAX 来解析 InvoiceConfiguration 中的 XML 文档

问题:

我想对 InvoiceConfiguration 进行单元测试。但我不希望 InvoiceConfiguration 从文件系统上物理上的 xml 文件中读取。我希望我的单元测试独立于任何物理存储的 xml 文件。我想在内存中创建一个 xml 表示。但是 StAX 解析器只需要 FileReader(或者我可以使用文件对象)

0 投票
14 回答
539092 浏览

java - "Content is not allowed in prolog" when parsing perfectly valid XML on GAE

I've been beating my head against this absolutely infuriating bug for the last 48 hours, so I thought I'd finally throw in the towel and try asking here before I throw my laptop out the window.

I'm trying to parse the response XML from a call I made to AWS SimpleDB. The response is coming back on the wire just fine; for example, it may look like:

I pass in this XML to a parser with

and call eventReader.nextEvent(); a bunch of times to get the data I want.

Here's the bizarre part -- it works great inside the local server. The response comes in, I parse it, everyone's happy. The problem is that when I deploy the code to Google App Engine, the outgoing request still works, and the response XML seems 100% identical and correct to me, but the response fails to parse with the following exception:

I have double, triple, quadruple checked this XML for 'invisible characters' or non-UTF8 encoded characters, etc. I looked at it byte-by-byte in an array for byte-order-marks or something of that nature. Nothing; it passes every validation test I could throw at it. Even stranger, it happens if I use a Saxon-based parser as well -- but ONLY on GAE, it always works fine in my local environment.

It makes it very hard to trace the code for problems when I can only run the debugger on an environment that works perfectly (I haven't found any good way to remotely debug on GAE). Nevertheless, using the primitive means I have, I've tried a million approaches including:

  • XML with and without the prolog
  • With and without newlines
  • With and without the "encoding=" attribute in the prolog
  • Both newline styles
  • With and without the chunking information present in the HTTP stream

And I've tried most of these in multiple combinations where it made sense they would interact -- nothing! I'm at my wit's end. Has anyone seen an issue like this before that can hopefully shed some light on it?

Thanks!

0 投票
2 回答
4475 浏览

java - XMLOutputStream,修复命名空间和没有命名空间的属性

一个简单的任务:编写一个元素的两个属性:

伍德斯托克斯的回答:

JDK 6 答案:

什么?!

此外,如果我们为元素添加前缀:

JDK 6 不再尝试发出 xmlns="":

如果我们删除一个属性(即只有一个),那很好。

我很确定这是 JDK 6 中的一个错误。对吗?任何人都可以提出一个可以让两个图书馆(和任何其他图书馆)都满意的解决方法吗?如果我能提供帮助,我不想要求使用woodstox。

0 投票
2 回答
2920 浏览

java - XMLEventWriter 从头开始​​:如何发出 xmlns 属性?

我正在尝试使用 StAX API 中的 XMLEventWriter 从头开始​​编写 XML 文档。

我无法弄清楚如何获取要发出的默认命名空间属性。

例如,默认命名空间 URI 字符串是“ http://www.liquibase.org/xml/ns/dbchangelog/1.9 ”。我希望它以 xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9" 的形式出现在我的 XML 根元素中。

这里有什么神奇的食谱?XMLEventWriter.setDefaultNamespace() 不起作用。

谢谢,莱尔德

0 投票
5 回答
7023 浏览

java - XMLEventWriter:我如何告诉它写空元素?

我没有看到其中的选项javax.xml.stream.XMLEventWriterjavax.xml.stream.XMLOutputFactory以某种方式设置任何一个以便写入空元素(而不是显式的开始和结束元素对)。

我看到 Woodstox 有一个属性可以做到这一点,但它不是标准化的。

我是否缺少任何明显的方法来做到这一点?