繁体   English   中英

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

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

如何使用 DomDocument 或 SimpleXML 或任何其他库通过 PHP 更改 XML 的根元素的前缀?

我试过这样:

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

或者

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

但它似乎没有按预期工作。

我期望的结果应该是:

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

我不确定您是否能够像在此处尝试那样修改根节点。 命名空间 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"/>

获得您寻求的输出的一种可能的替代方法可能是添加一个新节点 - 将原始根中的所有子节点附加到它并删除原始节点。 诚然,它很hacky ,很可能存在更好的解决方案......

给定源 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>

然后,修改源 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() );

这导致以下输出:

<?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>

根元素是根元素。 您可以通过使用您选择的根元素创建一个新文档来更改它。

如果剩下的工作是从另一个文档导入所有根元素子元素,那就这样吧(SimpleXML 不适合,DOMDocument 是,请参阅DOMDocument::importNode并注意$deep标志)。

如果您实际上并不想更改根元素而是重写 XML,那么XMLReader / XMLWriter可能就是您要寻找的。

(您的问题并不完全清楚,因为它错过了示例的输入文档)

与参考 Q&A 进行比较你如何在 PHP 中解析和处理 HTML/XML? .

暂无
暂无

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

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