PHP Umbenennen von XML Tags klappt nicht einheitlich?

FrazeColder

Lt. Commander
Registriert
Okt. 2013
Beiträge
1.718
Moin zusammen,

ich stehe leider wieder vor einem Problem und diesmal bin ich ratlos. Da es bei einigen XML Dateien funktioniert und bei anderen wieder nicht...

Ich führe den gleichen Befehl auf beide XML Dateien aus. Einmal auf die onlinekeystore.xml Datei und auf die g2a.xml Datei. Allerdings funktioniert es bei g2a.xml nicht und bei onlinekeystore.xml hingegen doch. Allerdings lassen sich andere XML Tags auch nicht umbenennen, obwohl ich den gleichen Befehl aufrufe.

Hat jemand eine Idee, woran das liegt und wie ich das ändern/beheben kann?
Nachfolgend der Code:

Onlinekeystore:
Code:
$xml = file_get_contents('data/onlinekeystore.xml');
renameTags($xml, 'priceEUR', 'price', 'data/onlinekeystore.xml');

G2A:
Code:
$xml = file_get_contents('data/g2a.xml');
renameTags($xml, 'name', 'title', 'data/g2a.xml');

renameTags:
Code:
function renameTags($xml, $old, $new, $path){
    $dom = new DOMDocument();
    $dom->loadXML($xml);

    $nodes = $dom->getElementsByTagName($old);
    $toRemove = array();
    foreach ($nodes as $node) {
        $newNode = $dom->createElement($new);
        foreach ($node->attributes as $attribute) {
            $newNode->setAttribute($attribute->name, $attribute->value);
        }

        foreach ($node->childNodes as $child) {
            $newNode->appendChild($node->removeChild($child));
        }

        $node->parentNode->appendChild($newNode);
        $toRemove[] = $node;
    }

    foreach ($toRemove as $node) {
        $node->parentNode->removeChild($node);
    }

    $dom->saveXML();
    $dom->save($path);
}

Onlinekeystore Input:
Code:
<product>
    <priceEUR>5.95</priceEUR>
</product>

Onlinekeystore Output:
Code:
<product>
   <price>5.95</price>
</product>

G2A Input:
Code:
<products>
    <name><![CDATA[1 Random STEAM PREMIUM CD-KEY]]></name>
</products>

G2A Output:
Code:
<products>
    <name><![CDATA[1 Random STEAM PREMIUM CD-KEY]]></name>
</products>

Ich würde mich sehr über Hilfe freuen!
Mit freundlichen Grüßen und Vielen Dank
 
Hm, funktioniert bei mir wunderbar.
Code:
X:\~php\FrazeColder
λ php -v
PHP 7.1.0 (cli) (built: Dec  2 2016 05:24:57) ( ZTS MSVC14 (Visual C++ 2015) x64 )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies

Code:
<?xml version="1.0" encoding="utf-8"?>
<asd>
	<product>
		<priceEUR>5.95</priceEUR>
	</product>
	<product>
		<priceEUR>6.95</priceEUR>
	</product>
</asd>
Code:
<?xml version="1.0" encoding="utf-8"?>
<asd>
	<product>
		
	<price>5.95</price></product>
	<product>
		
	<price>6.95</price></product>
</asd>

Code:
<?xml version="1.0" encoding="utf-8"?>
<asd>
	<products>
		<name><![CDATA[2 Random STEAM PREMIUM CD-KEY]]></name>
	</products>
	<products>
		<name><![CDATA[1 Random STEAM PREMIUM CD-KEY]]></name>
	</products>
</asd>
Code:
<?xml version="1.0" encoding="utf-8"?>
<asd>
	<products>
		
	<title><![CDATA[2 Random STEAM PREMIUM CD-KEY]]></title></products>
	<products>
		
	<title><![CDATA[1 Random STEAM PREMIUM CD-KEY]]></title></products>
</asd>
 
woran kann das liegen?
bei mir macht er das nicht...

liegt das evtl daran, weil die XML Daten unterschiedlichen aufgebaut sind?

Onlinekeystore (Input):
Code:
<?xml version="1.0" encoding="utf-8"?>
<products>
  <product>
    <sku>SKU2</sku>
    <title>Battlefield 2 Complete Collection CD KEY (Direct Download)</title>
    <url>http://www.onlinekeystore.com/battlefield-2-complete-collection-cd-key-origin.html</url>
    <qty>0</qty>
    <priceEUR>5.95</priceEUR>
    <priceUSD>6.23</priceUSD>
    <priceGBP>5.07</priceGBP>
    <priceAUD>8.67</priceAUD>
    <priceSGD>9.03</priceSGD>
  </product>

G2A (Input):
Code:
<?xml version="1.0" encoding="UTF-8"?>
<offers>
  <products>
    <name><![CDATA[1 Random STEAM PREMIUM CD-KEY]]></name>
    <description><![CDATA[shorted]]></description>
    <price>0.99</price>
    <category><![CDATA[All products,All,Steam,Sort List,Random steam,Seleccion,Kingsday items,TOP 10 Argentina,Les offres de la rentrée,De beste games tijdens Sinterklaas,MEJORESJUEGOS2016]]></category>
    <picture><![CDATA[https://images.g2a.com/m/230x336/0x1x1/small_image/s/t/11457d3cd8bb_steam_1_premium.jpg]]></picture>
    <delivery_time>0</delivery_time>
    <url><![CDATA[censored]]></url>
    <productid>25597</productid>
  </products>
 
Auch die gehen bei mir :confused_alt:
Code:
<?xml version="1.0" encoding="utf-8"?>
<products>
  <product>
    <sku>SKU2</sku>
    <title>Battlefield 2 Complete Collection CD KEY (Direct Download)</title>
    <url>http://www.onlinekeystore.com/battlefield-2-complete-collection-cd-key-origin.html</url>
    <qty>0</qty>
    
    <priceUSD>6.23</priceUSD>
    <priceGBP>5.07</priceGBP>
    <priceAUD>8.67</priceAUD>
    <priceSGD>9.03</priceSGD>
  <price>5.95</price></product>
  <product>
    <sku>SKU3</sku>
    <title>Battlefield 2 Complete Collection CD KEY (Direct Download)</title>
    <url>http://www.onlinekeystore.com/battlefield-2-complete-collection-cd-key-origin.html</url>
    <qty>0</qty>
    
    <priceUSD>6.23</priceUSD>
    <priceGBP>5.07</priceGBP>
    <priceAUD>8.67</priceAUD>
    <priceSGD>9.03</priceSGD>
  <price>5.95</price></product>
</products>

Code:
<?xml version="1.0" encoding="UTF-8"?>
<offers>
  <products>
    
    <description><![CDATA[shorted]]></description>
    <price>0.99</price>
    <category><![CDATA[All products,All,Steam,Sort List,Random steam,Seleccion,Kingsday items,TOP 10 Argentina,Les offres de la rentrée,De beste games tijdens Sinterklaas,MEJORESJUEGOS2016]]></category>
    <picture><![CDATA[https://images.g2a.com/m/230x336/0x1x1/small_image/s/t/11457d3cd8bb_steam_1_premium.jpg]]></picture>
    <delivery_time>0</delivery_time>
    <url><![CDATA[censored]]></url>
    <productid>25597</productid>
  <title><![CDATA[1 Random STEAM PREMIUM CD-KEY]]></title></products>
  <products>
    
    <description><![CDATA[shorted]]></description>
    <price>0.99</price>
    <category><![CDATA[All products,All,Steam,Sort List,Random steam,Seleccion,Kingsday items,TOP 10 Argentina,Les offres de la rentrée,De beste games tijdens Sinterklaas,MEJORESJUEGOS2016]]></category>
    <picture><![CDATA[https://images.g2a.com/m/230x336/0x1x1/small_image/s/t/11457d3cd8bb_steam_1_premium.jpg]]></picture>
    <delivery_time>0</delivery_time>
    <url><![CDATA[censored]]></url>
    <productid>25597</productid>
  <title><![CDATA[2 Random STEAM PREMIUM CD-KEY]]></title></products>
</offers>


Muss es denn unbedingt XML sein?
Wie waere es wenn du nur das aus dem XML ziehst was du benoetigst und dann so verarbeitest wie du es moechtest, anstatt die Dateien umzuwurschteln?
 
Zurück
Oben