user1131409
user1131409

Reputation: 29

how to parse this xml data using php?

<?xml version="1.0" encoding="UTF-8"?>
<ALEXA VER="0.9" URL="infosys.com/" HOME="0" AID="=">

   <RLS PREFIX="http://" more="62">
   <RL HREF="wipro.com/" TITLE="Wipro Corporation"/>
   <RL HREF="tcs.com/" TITLE="Tata Consultancy Services"/>
   <RL HREF="satyam.com/" TITLE="Satyam Computer Services Ltd"/>
   <RL HREF="ibm.com/" TITLE="IBM Corporation"/>
   <RL HREF="rediff.com/" TITLE="Rediff.com India Ltd."/>
   <RL HREF="moneycontrol.com/" TITLE="MoneyControl.com"/>
   <RL HREF="in.com/" TITLE="IN.com"/>
   <RL HREF="google.co.in/" TITLE="Google India"/>
   <RL HREF="www.stiknowledge.com/" TITLE="Business Process Outsourcing | Help Desk    Outsourcing And Certification"/>
   <RL HREF="www.sourcinginterests.org/" TITLE="Sourcing Interests Group"/>
   <RL HREF="www.dice.com/" TITLE="www.dice.com/"/>
   </RLS>
   <SD TITLE="A" FLAGS="DMOZ" HOST="infosys.com">
   <TITLE TEXT="Infosys"/>
   <ADDR STREET=" 44, Electronics City,  Hosur Road" CITY=" Bangalore,  KARNATAKA  560 100" STATE="" ZIP="" COUNTRY=" India" />
   <CREATED DATE="17-Jul-1992" DAY="17" MONTH="07" YEAR="1992"/>
   <PHONE NUMBER="91 80 852 0261"/>
   <OWNER NAME="Infosys Technologies Limited"/>
   <EMAIL ADDR="[email protected]"/>
   <LANG LEX="en" CODE="us-ascii"/>
   <LINKSIN NUM="2858"/>
  <SPEED TEXT="1308" PCT="55"/>
  <REVIEWS AVG="5.0" NUM="1"/>
  <CHILD SRATING="0"/>
   </SD>

   <KEYWORDS>
   <KEYWORD VAL="Karnataka"/>
   <KEYWORD VAL="Bangalore"/>
   </KEYWORDS><DMOZ>
   <SITE BASE="infosys.com/" TITLE="Infosys" DESC="Infosys (NASDAQ:INFY) defines, designs and delivers IT enabled business solutions. These provide you with strategic differentiation and operational superiority, thereby increasing your competitiveness. Each solution is delivered with the industry-benchmark Infosys Predictability that gives you peace of mind.">
   <CATS>
   <CAT ID="Top/Computers/Software/Consultants" TITLE="Software/Consultants" CID="379688"/>
  <CAT ID="Top/Regional/Asia/India/Karnataka/Localities/Bangalore/Business_and_Economy/Computers_and_Internet/Software" TITLE="Computers and Internet/Software" CID="497626"/>
  </CATS>
  </SITE>
  </DMOZ>
  <SD>
  <POPULARITY URL="infosys.com/" TEXT="15422"/>
  <REACH RANK="17335"/>
  <RANK DELTA="+1"/>
   </SD>
   </ALEXA>

i have used $xml= new SimpleXMLElement('xml data here') then used $x=$xml->xpath('/ALEXA); i am getting how to access LINKSIN NUM and REACH RANK here. if i send the $x in foreach like foreach($x as $y){ $y->LINKSIN NUM; } it will throw the error since there is a gap between LINKSIn and NUM

Upvotes: 0

Views: 531

Answers (2)

Rene Pot
Rene Pot

Reputation: 24815

This should work:

$xml = simplexml_load_string($dataHere);
$rank = (int)$xml->SD->REACH['RANK'];

Documentation: https://www.php.net/simplexml_load_string

If you have an URL for the xml:

$xml = simplexml_load_file($URL_to_file);

Upvotes: 3

dfsq
dfsq

Reputation: 193261

Try this:

$xml = simplexml_load_string($xml);
$links = $xml->xpath('//SD/LINKSIN');
$reach = $xml->xpath('//SD/REACH');

$num  = (int)$links[0]['NUM'];
$rank = (int)$reach[0]['RANK'];

Upvotes: 1

Related Questions