Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8683561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:04:34+00:00 2026-06-12T22:04:34+00:00

I have a SimpleXML Object made from merging multiple XMLs from PubMed (snippet below)

  • 0

I have a SimpleXML Object made from merging multiple XMLs from PubMed (snippet below) but there is repetition from the merge. How can I compare all first child arrays – array[][0], array[][1] etc – and discard any duplicates?
I though perhaps serialising was the answer but you can’t serialise a SimpleXML Object afaik..

I’m not sure where to start?

Array
(
  [0] => Array
    (
        [title] => SimpleXMLElement Object
            (
                [0] => Superstructure of the centromeric complex of TubZRC plasmid partitioning systems.
            )

        [link] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [Version] => 1
                    )

                [0] => 23010931
            )

        [author] => Aylett, CH., Löwe, J.
        [journal] => SimpleXMLElement Object
            (
                [0] => Proc. Natl. Acad. Sci. U.S.A.
            )

        [pubdate] => 2012-9-27
        [day] => SimpleXMLElement Object
            (
                [0] => 25
            )

        [month] => SimpleXMLElement Object
            (
                [0] => Sep
            )

        [year] => SimpleXMLElement Object
            (
                [0] => 2012
            )

    )
    [1] => Array
    (
        [title] => SimpleXMLElement Object
            (
                [0] => Superstructure of the centromeric complex of TubZRC plasmid partitioning systems.
            )

        [link] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [Version] => 1
                    )

                [0] => 23010931
            )

        [author] => Aylett, CH., Löwe, J.
        [journal] => SimpleXMLElement Object
            (
                [0] => Proc. Natl. Acad. Sci. U.S.A.
            )

        [pubdate] => 2012-9-27
        [day] => SimpleXMLElement Object
            (
                [0] => 25
            )

        [month] => SimpleXMLElement Object
            (
                [0] => Sep
            )

        [year] => SimpleXMLElement Object
            (
                [0] => 2012
            )

    )

Alternatively it could be done at the initial XML merge stage – I use the code below at the moment if anyone can suggest how to modify it to remove duplicates?

function simplexml_merge (SimpleXMLElement &$xml1, SimpleXMLElement $xml2) {
    $dom1 = new DomDocument();
    $dom2 = new DomDocument();

    $dom1->loadXML($xml1->asXML());
    $dom2->loadXML($xml2->asXML());

    $xpath = new domXPath($dom2);
    $xpathQuery = $xpath->query('/*/*');
    for ($i = 0; $i < $xpathQuery->length; $i++) {
        $dom1->documentElement->appendChild(
        $dom1->importNode($xpathQuery->item($i), true));
    }
    $xml1 = simplexml_import_dom($dom1);
}

$xml1 = new SimpleXMLElement($search1);
$xml2 = new SimpleXMLElement($search2);

simplexml_merge($xml1, $xml2);

Thanks.

…
…

For clarity – here’s the XML source layout that I am importing into SimpleXML – each PubmedArticle is one “element” I am interested in comparing and ensuring there are no duplicates –

    <xml...>
    <Document>
        <PubmedArticle>
            <MedlineCitation>
                <PMID version="1">xxx</PMID>
                ...
            </MedlineCitation>
            ...
        </PubmedArticle>
        <PubmedArticle>
            <MedlineCitation>
                <PMID version="1">xxx</PMID>
                ...
            </MedlineCitation>
            ...
        </PubmedArticle>
        etc
     </Document>
     </xml>

The PMID node is unique so can be used to check for duplicates.

…
…

Using the link from @Gordon – I know use:

//Get my source XML
$xml1 = new SimpleXMLElement($search1);
$xml2 = new SimpleXMLElement($search2);

//Run through $xml1 and build a query based on it's PMIDs
$query = array();
foreach ($xml1->PubmedArticle as $paper) {
    $query[] = sprintf('(PMID != %s)',$paper->MedlineCitation->PMID);
}
$query = implode('and', $query);

//Run through $xml2 and get node which don't have PMID matching $xml1
foreach ($xml2->xpath(sprintf('PubmedArticle/MedlineCitation[%s]', $query)) as $paper) {
    echo $paper->asXml();
}

However I still have one problem – getting the output merged.
The output of $xml2 is missing the <PubmedArticle> node around each ‘match’ for a start. Then I presume I can use the same merge code (above) to do the merge.
Can you point me in the right direction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:04:35+00:00Added an answer on June 12, 2026 at 10:04 pm

    Decided to follow @Gordon’s line as it kept it XML. Eventually got it all working:

    //function to check 2 xml inputs for duplicate nodes
        function dedupeXML($xml1, $xml2) {
            $query = array();
            foreach ($xml1->PubmedArticle as $paper) {
                $query[] = sprintf('(MedlineCitation/PMID != %s)',$paper->MedlineCitation->PMID);
            }
            $query = implode('and', $query);
    
            $xmlClean = '<Document>';
            foreach ($xml2->xpath(sprintf('PubmedArticle[%s]', $query)) as $paper) {
                $xmlClean .= $paper->asXML();
            }
            $xmlClean .= '</Document>';
            $xmlClean = new SimpleXMLElement($xmlClean);
            return $xmlClean;
        }
    //function to merge 2 xml inputs
        function mergeXML (SimpleXMLElement &$xml1, SimpleXMLElement $xml2) {
            // convert SimpleXML objects into DOM ones
            $dom1 = new DomDocument();
            $dom2 = new DomDocument();
            $dom1->loadXML($xml1->asXML());
            $dom2->loadXML($xml2->asXML());
            // pull all child elements of second XML
            $xpath = new domXPath($dom2);
            $xpathQuery = $xpath->query('/*/*');
            for ($i = 0; $i < $xpathQuery->length; $i++) {
                // and pump them into first one
                $dom1->documentElement->appendChild(
                $dom1->importNode($xpathQuery->item($i), true));
            }
            $xml = simplexml_import_dom($dom1);
            return $xml;
        }
    
        $xml1 = new SimpleXMLElement($search1);
        $xml2 = new SimpleXMLElement($search2);
        $xml3 = new SimpleXMLElement($search3);
        //dedupe and merge inputs
        //input 1 & 2
        $xml2Clean = dedupeXML($xml1, $xml2);
        $xml12 = mergeXML($xml1, $xml2Clean);
        //input 1+2 & 3
        $xml3Clean = dedupeXML($xml12, $xml3);
        $xml123 = mergeXML($xml12, $xml3Clean);
    

    This would be easy to adapt to other data sources – just modify the dedupeXML function to match the data structure of your XML.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simpleXml object and want to read the data from the object.I
I have this simplexml object and it looks fine, but when i convert a
I have a multi-dimensional multi-object array from a simplexml_import_dom() function call. A slice of
I have the following code which I use to load the xml object but
I have a codeigniter app that queryies an api and gets a simplexml object.
I have an object returned from $xml = simplexml_load_file($file). I'm trying to access 'location-id'
I have this SimpleXML object: object(SimpleXMLElement)#176 (1) { [record]=> array(2) { [0]=> object(SimpleXMLElement)#39 (2)
I have this object parsed using SimpleXML: SimpleXMLElement Object ( [contact] => SimpleXMLElement Object
Is it possible to use PHP's SimpleXML functions to create an XML object from
I have this simplexml result object: object(SimpleXMLElement)#207 (2) { [@attributes]=> array(1) { [version]=> string(1)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.