2017-12-11 9 views
1

나는이 당황하고있어 재미있는 부분은 내가 성공적으로이 코드를 여러 번 사용한 적이 있습니다 ... SimpleXML을 할 수 없습니다 xml의 ​​기존 노드에 문자열을 결합합니다. 아마 내가 '는 PHP를 통해 XML에 노드를 추가하고 (newsku)</p> <p>내 목표는 새 노드를 만드는 것입니다

... 무슨 일이 것은 내가 새 노드가없는 원본 XML을 얻을 수 있다는 것입니다

<?php 
    header('Content-Type: text/html; charset=UTF-8'); 
    error_reporting(E_ALL); 

    // Import test xml 

    $products = simplexml_load_file("http://test.com/xml/customer.xml"); 

    foreach($products->xpath("product") as $p) { 
     $p->addChild("newsku", "NEW" . $p->sku); 
    } 

    $products->asXML('test.xml'); 
    echo 'test XML files are updated'; 
?> 

: 여기

는 XML의 :

<products> 
    <product> 
     <id>3</id> 
     <name><![CDATA[ΜΙΚΡΟΦΩΝΟ SAMSON G-TRACK]]></name> 
     <manufacturer><![CDATA[SAMSON]]></manufacturer> 
     <sku><![CDATA[550.SAM.060]]></sku> 
     <description_greek><![CDATA[Samson G-Track - large diaphragm USB studio 
condenser microphone (USB bus-powered), built-in audio interface and mixer, 
allows simultaneous input of vocals and guitar, bass, or keyboard while also 
providing monitoring through an on-board headphone output. Specifications: mic 
and instrument/line gain control with clip LED, stereo input jacks for (3.5mm 
stereo-jack) instrument or line level signal, stereo headphone jack for zero 
latency monitoring with level control, 3-position headphone switch for stereo, 
mono and computer monitoring. USB bus-powered. Includes desktop microphone 
stand, audio I/O cables, USB cables and Cakewalk Sonar LE software. Optional 
shockmount available. 
]]></description_greek> 
     <short_description_greek><![CDATA[Samson G-Track - large diaphragm USB studio 
condenser microphone (USB bus-powered)]]></short_description_greek> 
     <price>155.00</price> 
     <msrp>185.00</msrp> 
     <instock>no</instock> 
     <images total="2"> 
      <image_1>http://test.com/media/catalog/product/5/5/550.sam.060-mi- 
01.jpg</image_1> 
      <image_2>http://test.com/media/catalog/product/5/5/550.sam.060-mi- 
02.jpg</image_2> 
     </images> 
    </product> 
</products> 

그리고 여기 내 코드입니다 나는 다른 많은 XML 파일에서 아무런 문제없이 이것을 사용했기 때문에 정말 어리석은 일을했다. ...

어떤 아이디어?

감사합니다.

+0

, 그것은으로 작동 예상 - https://3v4l.org/5JnEv –

+0

안녕 로렌스, 이유는 무엇입니까 다른 XML 파일이 아닌이이 작업 하나? –

+0

'http : // test.com/xml/customer.xml'에서 가져 와서'test.xml' : /에 저장하면 프로세스가 반복되기 때문에. –

답변

0

코드는 정상적으로 작동하지만 이 아닙니다.은 로컬 버전 test.xml 만 업데이트합니다. php 코드를 호출 할 때마다 동일한 값으로 덮어 씁니다.

다음 코드는 원격 소스에서 가져온 다음 newsku이라는 새 요소를 추가합니다. 그런 다음 브라우저로 출력하십시오.

$products = simplexml_load_file('http://test.com/xml/customer.xml'); 

foreach($products->xpath("*/product") as $p) { 
    $p->addChild("newsku", "NEW".$p->sku); 
} 

header('Content-Type: text/xml; charset=UTF-8'); 
// just outputs the xml - does not save 
exit($products->asXML()); 

여러 번이 먼저 파일을 저장 쓰기, XML을 캐시해야하는 경우. 이렇게하면 새로 고침 할 때마다 새 항목이 추가됩니다. /xml/customer.xml 만약

// the xml file name 
$xml_file = 'test.xml'; 

// check if the file exists or download and save it 
if (!file_exists($xml_file)) { 
    file_put_contents($xml_file, file_get_contents('http://test.com/xml/customer.xml')); 
} 

// load xml file for changes 
$products = simplexml_load_file($xml_file); 

foreach($products->xpath("*/product") as $p) { 
    $p->addChild("newsku", "NEW".$p->sku); 
} 

header('Content-Type: text/xml; charset=UTF-8'); 

// save the xml to file 
$products->asXML($xml_file); 

// read and display the xml file 
readfile($xml_file); 

은 서버에 실제로 지역이며, 내가 원격와 혼란 스러워요. 위의 코드를 fgc/fpc 파일없이 사용할 수 있습니다 (파일을 호출 할 때마다 업데이트됩니다).

// load xml file for changes 
$products = simplexml_load_file('/path/to/xml/customer.xml'); 

foreach($products->xpath("*/product") as $p) { 
    $p->addChild("newsku", "NEW".$p->sku); 
} 

header('Content-Type: text/xml; charset=UTF-8'); 

// save the xml to file 
$products->asXML($xml_file); 
readfile($xml_file); 

// or just outputs the xml - no saving 
// exit($products->asXML()); 

희망이 있습니다.

온라인을 참조하십시오 - https://3v4l.org/U8EbE

그것은 늘 원격 사이트의 버전을 업데이트
+0

안녕하세요. 로렌스와 시간 내 주셔서 감사합니다! 물론 원격 XML의 로컬 편집 버전을 저장하는 데 관심이 있습니다. 여전히 새 항목을 추가하지는 않습니다. 다음은 실제 원격 파일 url을 테스트하여 작동하는지 확인하는 URL입니다. https://www.rihardos.gr/index.php?option=com_vmxmlexporter&task=feed.export&id=7&type=xml&key=rihardos –

+0

''래퍼의 경우, 대신 $ products-> xpath ("*/product")'를 사용하십시오. –

+0

한번은 upvote를 잊지 마세요. –