SimpleXML根节点前缀php
我正在尝试使用php创建XML: 这是真正的XMLSimpleXML根节点前缀php,php,xml,xml-simple,Php,Xml,Xml Simple,我正在尝试使用php创建XML: 这是真正的XML <p:FatturaElettronica versione="FPA12" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://microsoft.com/wsdl/types/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" versione="FPA12" > <FatturaElett
<p:FatturaElettronica versione="FPA12" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://microsoft.com/wsdl/types/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" versione="FPA12" >
<FatturaElettronicaHeader>
<DatiTrasmissione>
<IdTrasmittente>
<IdPaese>IT</IdPaese>
<IdCodice>01234567890</IdCodice>
</IdTrasmittente>
<ProgressivoInvio>00001</ProgressivoInvio>
<FormatoTrasmissione>FPA12</FormatoTrasmissione>
<CodiceDestinatario>AAAAAA</CodiceDestinatario>
</DatiTrasmissione>
信息技术
01234567890
00001
FPA12
AAAAA
我的代码:
$xml = new SimpleXMLElement('<p:FatturazioneElettronica xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://microsoft.com/wsdl/types/" />');
$xml->addAttribute("versione","FPA12");
$xml->addAttribute("xmlns:xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
$FatturaElettronicaHeader = $xml->addChild('FatturaElettronicaHeader');
$DatiTrasmissione=$FatturaElettronicaHeader->addChild('DatiTrasmissione');
$IdTrasmittente=$DatiTrasmissione->addChild('IdTrasmittente');
$IdTrasmittente->addChild('IdPaese', 'IT');
$IdTrasmittente->addChild('IdCodice','01234567890');
$ProgressivoInvio=$DatiTrasmissione->addChild('ProgressivoInvio', '00001');
$FormatoTrasmissione=$DatiTrasmissione->addChild('DatiTrasmissione', 'FPA12');
$CodiceDestinatario=$DatiTrasmissione->addChild('CodiceDestinatario', 'AAAAAA');
$xml=新的SimpleXMLElement(“”);
$xml->addAttribute(“versione”、“FPA12”);
$xml->addAttribute(“xmlns:xmlns:xsi”http://www.w3.org/2001/XMLSchema-instance");
$FatturaElettronicaHeader=$xml->addChild('FatturaElettronicaHeader');
$DATITRASMISSISONE=$FATTURALETTRONICAHEADER->addChild('DATITRASMISSISONE');
$idtrasmittte=$DatiTrasmissione->addChild('idtrasmittte');
$idtrasmittte->addChild('IdPaese','IT');
$idtrasmittte->addChild('IdCodice','01234567890');
$ProgressivoInvio=$DatiTrasmissione->addChild('ProgressivoInvio','00001');
$FORMATOTRAMISSIONE=$DATITRAMISSIONE->addChild('DATITRAMISSIONE','FPA12');
$CodiceDestinatario=$DatiTrasmissione->addChild('CodiceDestinatario','AAAAAA');
在我的xml文件中,每个标记中都有前缀p:
我需要在根节点中有前缀p(p:FatturaElettronica)
我不知道怎么做
<p:FatturazioneElettronica xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://microsoft.com/wsdl/types/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" versione="FPA12">
<p:FatturaElettronicaHeader>
<p:DatiTrasmissione>
<p:IdTrasmittente>
<p:IdPaese>IT</p:IdPaese>
<p:IdCodice>01234567890</p:IdCodice>
</p:IdTrasmittente>
<p:ProgressivoInvio>00001</p:ProgressivoInvio>
<p:DatiTrasmissione>FPA12</p:DatiTrasmissione>
<p:CodiceDestinatario>AAAAAA</p:CodiceDestinatario>
</p:DatiTrasmissione>
信息技术
01234567890
00001
FPA12
AAAAA
SimpleXML的问题是,如果在添加元素时未指定元素的名称空间,它将采用父节点的名称空间(因此为p:
)。要将其添加到默认名称空间(即不带前缀),您需要更改以下几项
首先是在根元素添加一个默认名称空间声明
$xml = new SimpleXMLElement('<p:FatturazioneElettronica
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:p="http://microsoft.com/wsdl/types/"
xmlns="http://dummy.com" />');
我不明白我必须更改什么,抱歉,但我在这个主题上缺乏经验,对延迟表示担忧-您现在已经整理好了吗?不,我不明白我必须更改代码中的哪些内容。如果您查看答案中的代码行,并将其与您已有的代码行进行比较,你应该看到它们的区别。我必须只获取那些名称空间
$FatturaElettronicaHeader = $xml->addChild('FatturaElettronicaHeader',
null, 'http://dummy.com');