Title: Parent element data
Last modified: August 21, 2016

---

# Parent element data

 *  Resolved [adijeff](https://wordpress.org/support/users/adijeff/)
 * (@adijeff)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/parent-element-data/)
 * My XML is structured like this (simplified):
 * <campaign>
    <image>[http://website.com/image.jpg</image&gt](http://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://wordpress.org/plugins/wp-all-import/](https://wordpress.org/plugins/wp-all-import/)

Viewing 1 replies (of 1 total)

 *  Thread Starter [adijeff](https://wordpress.org/support/users/adijeff/)
 * (@adijeff)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/parent-element-data/#post-5013007)
 * 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.

 * ![](https://ps.w.org/wp-all-import/assets/icon-256x256.png?rev=2570179)
 * [WP All Import – Drag & Drop Import for CSV, XML, Excel & Google Sheets](https://wordpress.org/plugins/wp-all-import/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-all-import/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-all-import/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-all-import/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-all-import/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-all-import/reviews/)

## Tags

 * [parent](https://wordpress.org/support/topic-tag/parent/)
 * [wpallimport](https://wordpress.org/support/topic-tag/wpallimport/)
 * [xml](https://wordpress.org/support/topic-tag/xml/)

 * 1 reply
 * 1 participant
 * Last reply from: [adijeff](https://wordpress.org/support/users/adijeff/)
 * Last activity: [11 years, 10 months ago](https://wordpress.org/support/topic/parent-element-data/#post-5013007)
 * Status: resolved