简体   繁体   English

使用 PHP 更改 XML 根元素的前缀

[英]Change the prefix of the root element of an XML with PHP

How can I change the prefix of the root element of an XML with PHP using DomDocument or SimpleXML or any other library?如何使用 DomDocument 或 SimpleXML 或任何其他库通过 PHP 更改 XML 的根元素的前缀?

I tried like this:我试过这样:

$xml = new \SimpleXMLElement($content, \LIBXML_PARSEHUGE, false, "p", true);

or或者

$dom = new \DOMDocument();
    $response = $dom->loadXML($content);
    $root = $dom->documentElement;
    $root->setAttributeNS(
        'http://www.w3.org/2000/xmlns/',
        'xmlns:p',
        'p'
    );

But it doesn't seem to work as expected.但它似乎没有按预期工作。

The results that I expect should be:我期望的结果应该是:

<p:rootElement>
  <other>tag</other>
</p:rootElement>

I'm not sure that you would be able to modify the root node as you are trying to do here.我不确定您是否能够像在此处尝试那样修改根节点。 The namespace URI should be http://www.w3.org/2000/xmlns/p if it is p that you are trying to use as the prefix and you would use p:root as the qualifiedName but that would result in the root node being modified like this <root xmlns:p="http://www.w3.org/2000/xmlns/p" p:root="p"/>命名空间 URI 应为http://www.w3.org/2000/xmlns/p如果您尝试使用它作为前缀p并且您将使用p:root作为qualifiedName 但这将导致根像这样修改节点<root xmlns:p="http://www.w3.org/2000/xmlns/p" p:root="p"/>

A possible alternative approach to get the output you seek might be to add a new node - append all the child nodes from the original root to it and delete the original.获得您寻求的输出的一种可能的替代方法可能是添加一个新节点 - 将原始根中的所有子节点附加到它并删除原始节点。 Admittedly it is hacky and a better solution might well exist...诚然,它很hacky ,很可能存在更好的解决方案......

Given the source XML as:给定源 XML 为:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<root>
  <domain>cdiinvest.com.br</domain>
  <domain>savba.sk</domain>
  <domain>laboratoriodiseno.cl</domain>
  <domain>upc.ro</domain>
  <domain>nickel.twosuperior.co.uk</domain>
  <domain>hemenhosting.org</domain>
  <domain>hostslim.eu</domain>
</root>

Then, to modify the source XML:然后,修改源 XML:

    # local XML source file
    $file='xml/netblock.xml';
    
    # new node namespace properties
    $nsuri='http://www.w3.org/2000/xmlns/p';
    $nsname='p:root';
    $nsvalue=null;
    
    # create the DOMDocument and load the XML
    libxml_use_internal_errors( true );
    $dom=new DOMDocument;
    $dom->formatOutput=true;
    $dom->validateOnParse=true;
    $dom->preserveWhiteSpace=false;
    $dom->strictErrorChecking=false;
    $dom->recover=true;
    $dom->load( $file );
    libxml_clear_errors();
    
    # create the new root node
    $new=$dom->createElementNS( $nsuri, $nsname, $nsvalue );
    $dom->appendChild( $new );
    
    # find the original Root
    $root=$dom->documentElement;

    # iterate backwards through the childNodes - inserting into the new root container in the original order
    for( $i=$root->childNodes->length-1; $i >= 0; $i-- ){
        $new->insertBefore( $root->childNodes[ $i ],$new->firstChild );
    }

    # remove original root
    $root->parentNode->removeChild( $root );
    
    # save the XML ( or, as here, print out modified XML )
    printf('<textarea cols=80 rows=10>%s</textarea>', $dom->saveXML() );

Which results in the following output:这导致以下输出:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<p:root xmlns:p="http://www.w3.org/2000/xmlns/p">
  <domain>cdiinvest.com.br</domain>
  <domain>savba.sk</domain>
  <domain>laboratoriodiseno.cl</domain>
  <domain>upc.ro</domain>
  <domain>nickel.twosuperior.co.uk</domain>
  <domain>hemenhosting.org</domain>
  <domain>hostslim.eu</domain>
</p:root>

The root element is the root element.根元素是根元素。 You change it by creating a new document with the root element of your choice.您可以通过使用您选择的根元素创建一个新文档来更改它。

If the the job is left to import all root elements children from another document, then be it (SimpleXML is not that fitting for that, DOMDocument is, see DOMDocument::importNode and mind the $deep flag).如果剩下的工作是从另一个文档导入所有根元素子元素,那就这样吧(SimpleXML 不适合,DOMDocument 是,请参阅DOMDocument::importNode并注意$deep标志)。

If you don't actually want to change the root element but rewrite the XML then XMLReader / XMLWriter is likely what you're looking for.如果您实际上并不想更改根元素而是重写 XML,那么XMLReader / XMLWriter可能就是您要寻找的。

(This is not entirely clear with your question as it misses the input document of the example) (您的问题并不完全清楚,因为它错过了示例的输入文档)

Compare as well with the reference Q&A How do you parse and process HTML/XML in PHP?与参考 Q&A 进行比较你如何在 PHP 中解析和处理 HTML/XML? . .

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

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