You are here:  » remove duplicates in xml


remove duplicates in xml

Submitted by nonibanacu on Mon, 2017-10-30 09:31 in

Considering the xml below, first and last entries are duplicates for me based on the first 3 xml fields.

<xml>
  <moto>
    <id>1</id>
    <name>test</name>
    <value>34</value>
    <other>nnn</other>
  </moto>
  <moto>
    <id>2</id>
    <name>any other</name>
    <value>sample</value>
    <other>xxx</other>
  </moto>
  <moto>
    <id>1</id>
    <name>test</name>
    <value>34</value>
    <other>nnn</other>
  </moto>
</xml>

How can process the xml in a fast way, such that the xml transforms into the example below. Basically removing one of the considered duplicates.

<xml>
  <moto>
    <id>1</id>
    <name>test</name>
    <value>34</value>
    <other>nnn</other>
  </moto>
  <moto>
    <id>2</id>
    <name>any other</name>
    <value>sample</value>
    <other>xxx</other>
  </moto>
</xml>

Submitted by support on Mon, 2017-10-30 10:26

Hi,

You can do this easily by creating an array of id's already processed, and skipping any that are duplicated, for example;

$ids = array();
function myRecordHandler($record)
{
  global $ids;
  if (in_array($ids[$record["ID"]],$ids)) return;
  $ids[$ids[$record["ID"]]] = $ids[$record["ID"]];
  // process $record as normal here
}
MagicParser_parse("moto.xml","myRecordHandler","xml|XML/MOTO/");

Hope this helps!
Cheers,
David
--
MagicParser.com