简体   繁体   English

是否可以使用DomDocument创建一个元素,其中开始和结束标签不同?

[英]Is it possible to create an element with DomDocument where the opening and closing tags are different?

I know this sounds odd but I have a special situation where I need to create an xml (actually this won't be an xml document anymore because of the different closing tag) where the root opening tag is different from the root closing tag. 我知道这听起来很奇怪,但是在特殊情况下,我需要创建一个xml(由于关闭标签不同,因此不再是xml文档),其中根开始标签与根结束标签不同。 Like: 喜欢:

<OPENING_TAG>
   <ONE></ONE>
   <TWO></TWO>
   <THREE></THREE>
</CLOSING_TAG>

Again I know this is not right but as I have mentioned this is a special case where the opening and closing tags have to be different. 我再次知道这是不对的,但是正如我已经提到的,这是一个特殊情况,其中开始和结束标签必须不同。 How can I accomplish that with DomDocument? 如何使用DomDocument完成此操作?

What you seem to want to do is create an invalid XML document. 您似乎想要做的是创建一个无效的XML文档。 DOMDocument won't allow you to do that - it makes no sense. DOMDocument不允许您这样做-毫无意义。 SO the answer is No you can't. 所以答案是不,你不能。

If you want to create the document your after, create it as text directly. 如果要创建文档,请直接将其创建为文本。

No, DOM doesn't allow invalid XML document. 不,DOM不允许无效的XML文档。 If it is necessary to create opening and closing tags, you can write xml like below (if it suits your requirement): 如果有必要创建开始和结束标签,则可以像下面这样编写xml(如果它适合您的要求):

<MAIN_TAG>
   <OPENING_TAG></OPENING_TAG>
   <ONE></ONE>
   <TWO></TWO>
   <THREE></THREE>
   <CLOSING_TAG></CLOSING_TAG>
<MAIN_TAG>

Or you can create it as a simple string (text). 或者,您可以将其创建为简单的字符串(文本)。

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

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