简体   繁体   English

在Java中将原始XML元素动态读取为文本

[英]Dynamically reading raw XML elements as text in Java

Assuming an XML file with unknown structure (ie, unknown element and attribute names), like 假设XML文件具有未知结构(即未知元素和属性名称),例如

<RootElement>
   <Level 1 ...>
        <Level 2 ...>
            ...
        </Level 2>
        <Level 2 ...>
            ...
        </Level 2>
    </Level 1>
    <Level 1 ...>
        <Level 2 ...>
            ...
        </Level 2>
        <Level 2 ...>
            ...
        </Level 2>
    </Level 1>
</RootElement>

Is there any way using StAX to get the full raw text of each element? 有没有办法使用StAX来获取每个元素的完整原始文本

At least, how can this be done for the first level, ie in the above example (ignoring pretty printing) how can we read the following 2 strings in a Java String variable: 至少,如何在第一级完成,即在上面的例子中(忽略漂亮的打印)我们如何在Java String变量中读取以下2个字符串:

"<Level 1 ...><Level 2...>...</Level 2></Level 1>"

and

"<Level 1 ...><Level 2...>...</Level 2></Level 1>"

Use an XMLStreamReader and XMLStreamWriter together to get (producee) whatever raw XML you want to. 使用XMLStreamReader和XMLStreamWriter一起获取(生成)您想要的任何原始XML。 It might seem like you can do some tricks for a more simple solution, but you can't - the XML needs to be parsed or else you are in deep water, and if you'd like to hack a parser, they are usually implemented with internal buffering which makes it a bit of hairy work to correctly cut up an incoming stream. 看起来你可以为一个更简单的解决方案做一些技巧,但你不能 - 需要解析XML,否则你处于深水中,如果你想破解解析器,通常会实现它们内部缓冲使得正确切割传入流有点毛茸茸的工作。

Edit: Use the parsing pattern in this question to keep track of the level. 编辑:使用此问题中的解析模式来跟踪级别。 To write, handle each event type from the input in its own way - note that you can iterator over all the attributes and also namespaces for start element events. 要编写,以自己的方式处理输入中的每个事件类型 - 请注意,您可以对start元素事件的所有属性和命名空间进行迭代。

No, XMLStreamReader allows you to get the text content of a text only xml node with getElementText() , to get the full content you will have to read the file yourself and grab the elements and reconstruct the XML. 不, XMLStreamReader允许您使用getElementText()获取仅文本 xml节点的文本内容 ,以获取您必须自己读取文件的完整内容并获取元素并重新构建XML。

But maybe what you want to do is something else. 但也许你想要做的就是别的。 Why don't you explain why you need this? 你为什么不解释为什么需要这个?

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

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