我有一些XML,需要为它设置命名空间。 这是我尝试的方式:
$classXML = "<CLASSXml><CalculationIndex><RunDesc>NormalCalc</RunDesc></CalculationIndex><GlobalData><CalcIdent><CalcUId>CLASS:20151203:19335012:000000000000:203:CS:CZK:349074</CalcUId><CalcNo>2454307111</CalcNo><CustomNo>349074</CustomNo><CalcVer>2300</CalcVer><XMLVer>23.00.01</XMLVer><ClassBuild>23.00.04.03</ClassBuild><SAXIFVersion>6.2</SAXIFVersion><SystemDat>2015-12-03</SystemDat><TimeStamp>19:33:50.12</TimeStamp><ProdMasterVer>0</ProdMasterVer></CalcIdent><CalcParam><CalcDat>2015-12-03</CalcDat><PPPriceDat>2015-11-01</PPPriceDat><NumbersOfLbrRt Val=\"1\"/><CountryCdISO>203</CountryCdISO><LngCdISO>CS</LngCdISO><ManufacturerTimeBase>5</ManufacturerTimeBase><TimeBase Unit=\"WU/h\" Val=\"10\"/><TimeBaseDtl><DecNo Val=\"1\"/></TimeBaseDtl><UcsWoMat>true</UcsWoMat><AZTCalcPrepCd>0</AZTCalcPrepCd><SGNFlag>1</SGNFlag><PPPriceDatMaster>2015-11-01</PPPriceDatMaster><MaintCd>M</MaintCd><CurDtl><ISO>CZK</ISO><DecNo Val=\"2\"/><Desc>CZK</Desc></CurDtl><RecordId>7</RecordId></CalcParam><Vehicle><VehicleIdentification><VIN>12365478965412365</VIN><ManufacturerCodeAX>07</ManufacturerCodeAX><ManufacturerName>ALFA ROMEO</ManufacturerName><ModelCodeAX>25</ModelCodeAX><ModelName>SPIDER</ModelName><SubModelCodeAX>52</SubModelCodeAX><SubModelName>EXCLUSIVE</SubModelName><ModelCdAXIntern>07</ModelCdAXIntern></VehicleIdentification><VehicleModelOptions><Known><ModelOptionDetail><MO>W3</MO><MODesc>3-DVEŘOVÉ (COUPE)</MODesc><MOSrcCd>1</MOSrcCd><MOPrtCd>0</MOPrtCd><MOFncCd>Z0</MOFncCd><MOGrpCd>100</MOGrpCd></ModelOptionDetail></Known></VehicleModelOptions></Vehicle></GlobalData><CalcData><RunDesc>NormalCalc</RunDesc><FinalCalc><GrandTotal><RepTaxDtl></RepTaxDtl><TotTaxDtl></TotTaxDtl><TaxPC Unit=\"%\" Val=\"+21.0\"/></GrandTotal><FCRepTot><RepTotTaxDtl></RepTotTaxDtl></FCRepTot><FCPart><FCSundry></FCSundry></FCPart><FCLabor></FCLabor><FCAdditionalCost></FCAdditionalCost><FCPaint></FCPaint></FinalCalc><SpareParts></SpareParts><Labor></Labor><ExplanatoryTexts><ExplTxt><Desc>CENY ND = NCDV</Desc><Desc>* = ÚDAJE UŽIVATELE</Desc></ExplTxt><ExplTxt><Desc>NN = NEEXISTUJE Č ND / Č PRAC-POZ</Desc><Desc>Č. ND NEMUSÍ BÝT OBJEDNACÍM Č</Desc></ExplTxt><ExplTxt><Desc>)=ČAS-JEDNOTKY V JINÉ POZICI OBSAŽENY</Desc><Desc>ZAX = ČAS-NORMU ZAJISTIL AUDATEX</Desc></ExplTxt></ExplanatoryTexts><CalcControl></CalcControl></CalcData></CLASSXml>";
$classXML=simplexml_load_string($classXML);
$classXML->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:g', 'http://base.google.com/ns/1.0');
print_r($classXML);
但是我收到错误
的SimpleXMLElement :: setAttributeNS()
在此之后,我尝试通过DOM加载XML,如:
$doc = new DOMDocument();
$doc->loadXML($classXML);
$classXML->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:g', 'http://base.google.com/ns/1.0');
同样的结果。 伙计们如何在PHP中设置XML加载的命名空间?
结果如何:
<g:CLASSXml><g:CalculationIndex><g:RunDesc>NormalCalc</g:RunDesc></g:CalculationIndex><g:GlobalData><g:CalcIdent>...and so on
感谢您的任何建议 Andurit
答案 0 :(得分:0)
$doc = new DOMDocument();
$doc->loadXML($classXML);
$rootNode = $doc->documentElement; // getting root node
$rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:g', 'http://base.google.com/ns/1.0');