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
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();

?>

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.