亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 xml php 中添加前綴字符串所有標簽

在 xml php 中添加前綴字符串所有標簽

PHP
aluckdog 2023-08-19 17:55:36
你好,我嘗試在我的 xml 標簽上添加前綴,如下所示 <identifiant>       <a:Nom>NOM</a:Nom>       <a:NomJeuneFille i:nil="true" />       <a:Prenom>PRENOM</a:Prenom>       </identifiant>誰能幫我
查看完整描述

2 回答

?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

這是一個 XML 命名空間前綴,如果沒有匹配的 XML 命名空間定義,它是無效的。DOM 有特殊的方法來創建帶有命名空間的 XML 節點:


// a list of namespaces just to avoid repeating the URIs

// the keys do not have to match the prefixes in the XML

$namespaces = [

  'xmlns' => 'http://www.w3.org/2000/xmlns/',

  'a' => 'urn:example:a',

  'i' => 'urn:example:i'

];


$document = new DOMDocument();

// the document element does not seem to have a namespace

$identifiant = $document->appendChild(

    $document->createElement('identifiant')

);

// add the namespace definition for prefix "a"

// namespace definition use a reserved, internal namespace

$identifiant->setAttributeNS($namespaces['xmlns'], 'xmlns:a', $namespaces['a']);


$identifiant->appendChild(

    // create an elment node with the namespace

    $document->createElementNS(

        $namespaces['a'],

        'a:Nom'

    )

)->textContent = 'NOM';


$identifiant

  ->appendChild(

    $document->createElementNS(

        $namespaces['a'],

        'a:NomJeuneFille'

    )

  )

  ->setAttributeNS(

        $namespaces['i'],

        'i:nil',

        'true'

  );



$identifiant->appendChild(

    $document->createElementNS(

        $namespaces['a'],

        'a:Prenom'

    )

)->textContent = 'PRENOM';


$document->formatOutput = TRUE;

echo $document->saveXML();

輸出:


<?xml version="1.0"?>

<identifiant xmlns:a="urn:example:a">

  <a:Nom>NOM</a:Nom>

  <a:NomJeuneFille xmlns:i="urn:example:i" i:nil="true"/>

  <a:Prenom>PRENOM</a:Prenom>

</identifiant>

將根據需要添加命名空間定義。您可以通過在祖先節點上手動定義它來避免在兄弟分支上重復它。我對“a”前綴執行此操作,“i”前綴的定義是自動添加的。


查看完整回答
反對 回復 2023-08-19
?
MYYA

TA貢獻1868條經驗 獲得超4個贊

嘗試對所選內容進行查找和替換(大多數文本編輯器允許您執行此操作)

如果您的文件看起來相似,您可以通過濫用以空格開頭的想法來以最小的努力替換開始塊

  • 尋找      <

  • 代替      <a:


如果這還不夠(即您有一個更復雜的文件或希望對大量文件執行此操作),您將需要使用 XML 解析器重排您的 XML 文檔

一個程序看起來像

import XML parsing library

read XML file

iterate over the blocks of whatever depth until you find what you want

change the block names (perhaps copy to new blocks and switch 'em)

write the resulting file again (consider writing to a new file so you can compare them more easily)



查看完整回答
反對 回復 2023-08-19
  • 2 回答
  • 0 關注
  • 194 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號