Quantcast
Channel: Topic Tag: xml | WordPress.org
Viewing all articles
Browse latest Browse all 3201

adijeff on "[Plugin: Import any XML or CSV File to WordPress] Parent element data"

$
0
0

Hi all,

I thought I would reply to my own post in case anyone else needs help with this. This is the code I used to amend the XML using PHP. I am not a coder, so this is possibly not the most efficient way, but it works!

Essentially, I read the XML file, and go through each <campaign> node, find the imageurl tag and then go through each <event> node within that <campaign> and add the imageurl into that node. Then instead of calling the original XML file, I call the new PHP file:

<?php
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = false;
$doc->load("xmlfile.xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
echo "<campaigns>";
$xpath = new DOMXpath($doc);
$searchNode = $xpath->query('//campaign'); // find campaign nodes
foreach( $searchNode as $searchNode )
{
     $valueID = $searchNode->getAttribute('id');
			echo "<campaign id=\"" .$valueID. "\">"; // get the campaign id
	//campaignimage
	$xmlUrl = $searchNode->getElementsByTagName( "campaignimage" );
    $valueImage = $xmlUrl->item(0)->nodeValue; // get the imageurl
	$searchEventNode = $xpath->query("campaign[@id='$valueID']//event"); // get all events within our selected campaign (using id)
	foreach( $searchEventNode as $searchEventNode )
	{
		//get venuelocation
		$xmlVenueLocation = $searchEventNode->getElementsByTagName( "venuelocation" );
		$valueVenueLocation = $xmlVenueLocation->item(0)->nodeValue;
		echo "<event>";
		echo "<campaignimage>" .$valueImage. "</campaignimage>";
		echo "<venuelocation>" .$valueVenueLocation. "</venuelocation>";
		echo "</event>";
	}
	echo "</campaign>";
}
echo "</campaigns>";
?>

Viewing all articles
Browse latest Browse all 3201

Trending Articles