• Resolved adijeff

    (@adijeff)


    My XML is structured like this (simplified):

    <campaign>
    <image>https://website.com/image.jpg</image&gt;
    <event>
    <location>Bristol</location>
    </event>
    <event>
    <location>Liverpool</location>
    </event>
    </campaign>

    If I use /campaign[events/event/venue/venuelocation[contains(.,”Liverpool”)]] as the xpath then the Bristol event is imported (first event in the campaign).

    If I use /event[venue/venuelocation[contains(.,”Liverpool”)]] as the xpath then the Liverpool event is imported correctly, but I cannot get the campaign image because it is parent of the <event> tags. Unfortunately, it is not possible to change the XML.

    Please can you advise? Thanks.

    https://www.ads-software.com/plugins/wp-all-import/

Viewing 1 replies (of 1 total)
  • Thread Starter adijeff

    (@adijeff)

    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 1 replies (of 1 total)
  • The topic ‘Parent element data’ is closed to new replies.