Jump to content

php find replace xml data


karl-phpfreaks

Recommended Posts

I have this code reads the xml runs with no errors but will not input the data from the array in the <catparent>

 

The script

 

<?php
// isbn => pages
$page_numbers = array(
                     '1234' => 'hello parent', // insert data into catparent node
                     '5678' => 'hello parent', // insert data into catparent node
                );
$dom = new DOMDocument();
$dom->load('edtest1.xml');
$xpath = new DOMXPath($dom);
$items = $xpath->query('item');
foreach($items as $item)
{
    $catcode = $item->getElementsByTagName['catcode'];
$parent_code = $page_numbers[ $catcode[0] ];
$item->appendChild( $dom->createElement('catparent', $parent_code) );
}
$dom->save('edtest_stack.xml');
?>

 

My XML file looks like

 

<?xml version="1.0" encoding="UTF-8"?>

<items>

<item>
<catcode>1234</catcode>
<catdesc>Systems - System Bundles</catdesc>
</item>

<item>
<catcode>5678</catcode>
<catdesc>Hard Drives - SATA</catdesc>
</item>
</items>

 

Output XML looks like this as you can see it has created the <catparent> but will not insert the data from the array.

 

<?xml version="1.0" encoding="UTF-8"?>
<items>

<item>
<catcode>1234</catcode>
<catdesc>Systems - System Bundles</catdesc>
<catparent></catparent></item>

<item>
<catcode>5678</catcode>
<catdesc>Hard Drives - SATA</catdesc>
<catparent></catparent></item>
</items>

 

Any ideas pointers as to where I'm going wrong would be most helpful

Link to comment
https://forums.phpfreaks.com/topic/252061-php-find-replace-xml-data/
Share on other sites

SimpleXML is an easier XML solution than DOM, IMO.

 

<?php

$xml_data = <<<HEREDOC
<?xml version="1.0" encoding="UTF-8"?>

<items>

<item>
<catcode>1234</catcode>
<catdesc>Systems - System Bundles</catdesc>
</item>

<item>
<catcode>5678</catcode>
<catdesc>Hard Drives - SATA</catdesc>
</item>
</items>
HEREDOC;

$page_numbers = array(
'1234' => 'hello parent1',
'5678' => 'hello parent2',
);

$xml = new SimpleXMLElement( $xml_data );

// Loop through all items in XML
foreach( $xml->item as $item ) {
// Check if 'catcode' is a child of this item
// and if $page_numbers has a matching key
// In order to use the value as an array key, we need to explicitly
// call it as a string, using typecasting (string)
if( isset($item->catcode) && isset($page_numbers[(string)$item->catcode]) )
	// Create a new child under this item, using the $page_numbers value
	$item->addChild( 'catparent', $page_numbers[(string)$item->catcode] );
}

// Output the XML
echo $xml->saveXML();

?>

I used this code to sort the problem it's not the best formed I don't think but it works.

 

Now I need to cure case sensitive issues

 

<?php

$xml = simplexml_load_file('edtest1.xml');

$page_numbers = array(
'SWOT' => 'Software',
'SWUT' => 'Software',
'VOGA' => 'VOIP',

);

// Loop through all items in XML
foreach( $xml->item as $item ) {
// Check if 'catcode' is a child of this item
// and if $page_numbers has a matching key
// In order to use the value as an array key, we need to explicitly
// call it as a string, using typecasting (string)
if( isset($item->catcode) && isset($page_numbers[(string)$item->catcode]) )
	// Create a new child under this item, using the $page_numbers value
	$item->addChild( 'catparent', $page_numbers[(string)$item->catcode] );
}

// Output the XML
echo $xml->saveXML(test.xml);
?>

If the keys of $page_numbers is always uppercase, You could simply use strtoupper to make the $item->catcode UPPERCASE.

 

Otherwise, I would first loop through $page_numbers and change all of the keys to lowercase (preference) and use strtolower on $item->catcode.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.