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 8908997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:19:32+00:00 2026-06-15T03:19:32+00:00

I have an XML file which is supposed to be my phones contacts backup

  • 0

I have an XML file which is supposed to be my phones contacts backup and I am trying to create a php file to retrieve only the contacts that have a phone number assigned to them. The file contains contacts from different applications.

The XML has these elements:

<Contact>
    <Id>5238</Id>
    <GivenName>friend1</GivenName>
    <FullName>friendA</FullName>
    <CreateTime>0001-01-01T00:00:00+00:00</CreateTime>
    <ModifyTime>0001-01-01T00:00:00+00:00</ModifyTime>
    <Starred>false</Starred>
    <AccountName>SIM</AccountName>
    <AccountType>com.anddroid.contacts.sim</AccountType>
  </Contact>

<PhoneNumbers>
    <Id>53</Id>
    <ContactId>1380</ContactId>
    <Name>2</Name>
    <Value>07123456789</Value>
    <Primary>2</Primary>
  </PhoneNumbers>

<Contact>
    <Id>328</Id>
    <FamilyName>tee</FamilyName>
    <GivenName>friend2</GivenName>
    <FullName>friend2 tee</FullName>
    <CreateTime>0001-01-01T00:00:00+00:00</CreateTime>
    <ModifyTime>0001-01-01T00:00:00+00:00</ModifyTime>
    <Picture>18948</Picture>
    <Starred>false</Starred>
    <AccountName>xxxxxxx@hotmail.com</AccountName>
    <AccountType>com.htc.socialnetwork.facebook</AccountType>
  </Contact>

And I want to make a php file that will retrieve the FullName from Contact and the Value from PhoneNumbers where the Contact/Id matches the PhoneNumbers/ContactId.

I created this code:

<?php
$xml = simplexml_load_file("Contact.xml");

$i=0;
$k=0;
foreach ($xml->Contact as $contact) {
    if ($contact->AccountName == "SIM"){

   echo "Contact:   " . $k . "<br />   "; echo $contact->nodeValue[$k] . "<br />   " . $contact->FullName . "<br />   ";
   $k++;
   }
   }
   foreach ($xml->PhoneNumbers as $number) {

    echo "Contact:   " . $i . "<br />   "; echo  $number->Value . "<br />   ";
   $i++;
    }

?>

It outputs 53 contacts and 173 numbers. If I dont put the if ($contact->AccountName == "SIM") it outputs the same numbers but 700++ contacts. I just want some help producing a function or something to output only the contacts that I already have their phone number.

Any help is appreciated.

Thank you

  • 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-15T03:19:34+00:00Added an answer on June 15, 2026 at 3:19 am

    I would suggest to use a XSL-stylesheet:

    <xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    
    <xsl:template match="/">
      <ul><xsl:apply-templates/></ul>
    </xsl:template>
    
    <xsl:template match="Contact"> 
      <!-- select phonenumbers with the matching ContactId -->
      <xsl:variable name="numbers" select="//PhoneNumbers[ContactId=current()/Id]"/>
    
      <!-- when any matching  PhoneNumber has been found, continue -->
      <xsl:if test="count($numbers) &gt; 0">
      <li>
        <xsl:value-of select="FullName"/>
        <ul>
          <!-- call a named template with the matching PhoneNumbers as param -->
          <xsl:call-template name="printNumbers">
            <xsl:with-param name="numbers" select="$numbers" />
          </xsl:call-template>
        </ul>
      </li>
      </xsl:if>
    </xsl:template>
    
    <xsl:template name="printNumbers">
      <xsl:param name="numbers" />
      <!-- loop through PhoneNumbers and print the Value -->
      <xsl:for-each select="$numbers">
        <li><xsl:value-of select="Value" /></li>
      </xsl:for-each>
    </xsl:template>
    
    <xsl:template match="PhoneNumbers"/>
    </xsl:stylesheet> 
    

    How to use the stylesheet:

    <?php
    
    $doc = new DOMDocument();
    $xsl = new XSLTProcessor();
    
    $doc->load('path/to/stylesheet.xsl');
    $xsl->importStyleSheet($doc);
    
    $doc->load('Contact.xml');
    echo $xsl->transformToXML($doc);
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I have a class createUser which is supposed to create an XML file
I have an XML file which I need to parse using PHP and send
I need to create an xml file that is supposed to look like this.
I have a xml file which I need to open with Microsoft Word 2007.
I have this xml file which has text init. i.e Hi my name is
I have an XML file which XML parser choke on. A part of it
I have an XML file which has many section like the one below: <Operations>
I have an XML file which I'd like to parse into a non-XML (text)
I have an XML file which has to be transformed using XSLT, I am
I have written xml file which contains html tags as element like <component> <input

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.