469,288 Members | 2,357 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,288 developers. It's quick & easy.

Read xml file from last entry

134 100+
hi

i have a xml file which has transaction details of the users.

I m inserting data using appendChild() from php.

While displaying i need to display the last 5 entry or single entry or the whole entries descending using php.

Is there anyway to read the xml file from the lastentry using php...


regards
vijay
Sep 2 '09 #1
16 3997
Dormilich
8,651 Expert Mod 8TB
you can read XML in PHP either by DOMDocument or SimpleXML
Sep 2 '09 #2
vjayis
134 100+
Yes i can.,

but i need to read the entries descendingly.. i cant figure it out from the definitions..

any suggestions..
Sep 2 '09 #3
Dormilich
8,651 Expert Mod 8TB
@vjayis
sorted or as they appear in the file? (the elements are usually sorted in document order, so you can read the NodeList and do any DOM functions with it that you need)

you may consider reading them into an array and apply any array functions you need. other than that you may use XSLT (which has a sort command).
Sep 2 '09 #4
vjayis
134 100+
here is my file contents

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <trans>
  3.     <transfer>
  4.         <id>1</id>
  5.         <acno>B900326</acno>
  6.         <amount>10000</amount>
  7.         <type>Credit</type>
  8.         <memo>(Credit)memo!@#$%^&amp;*()_+|}{":?&gt;&lt;</memo>
  9.         <fee>0</fee>
  10.         <ft_acno>Admin</ft_acno>
  11.         <batch>1</batch>
  12.         <tempbalance>10000</tempbalance>
  13.         <date>2009-07-11</date>
  14.         <time>1247288167</time>
  15.     </transfer>
  16.     <transfer>
  17.         <id>2</id>
  18.         <acno>P587178</acno>
  19.         <amount>98</amount>
  20.         <type>Credit</type>
  21.         <memo>test</memo>
  22.         <fee>2</fee>
  23.         <ft_acno>B900326</ft_acno>
  24.         <batch/>
  25.         <tempbalance>98</tempbalance>
  26.         <date>2009-07-11</date>
  27.         <time>1247288200</time>
  28.     </transfer>
  29.     <transfer>
  30.         <id>3</id>
  31.         <acno>B900326</acno>
  32.         <amount>100</amount>
  33.         <type>Debit</type>
  34.         <memo>test</memo>
  35.         <fee>0</fee>
  36.         <ft_acno>P587178</ft_acno>
  37.         <batch/>
  38.         <tempbalance>9900</tempbalance>
  39.         <date>2009-07-11</date>
  40.         <time>1247288200</time>
  41.     </transfer>
  42.     <transfer>
  43.         <id>4</id>
  44.         <acno>P662318</acno>
  45.         <amount>196</amount>
  46.         <type>Credit</type>
  47.         <memo>testing</memo>
  48.         <fee>4</fee>
  49.         <ft_acno>B900326</ft_acno>
  50.         <batch/>
  51.         <tempbalance>196</tempbalance>
  52.         <date>2009-07-13</date>
  53.         <time>1247460597</time>
  54.     </transfer>
  55.     <transfer>
  56.         <id>5</id>
  57.         <acno>B900326</acno>
  58.         <amount>200</amount>
  59.         <type>Debit</type>
  60.         <memo>testing</memo>
  61.         <fee>0</fee>
  62.         <ft_acno>P662318</ft_acno>
  63.         <batch/>
  64.         <tempbalance>9700</tempbalance>
  65.         <date>2009-07-13</date>
  66.         <time>1247460597</time>
  67.     </transfer>
  68.  </trans>
  69.  
there are totally 5entry's in it.

i need to display it in descending order.

I had found an way to display it in the descending order as follows..

Expand|Select|Wrap|Line Numbers
  1. <?
  2. $filename='test.xml';
  3.  
  4. $data = simplexml_load_file($filename);
  5.  
  6. $c=count($data->transfer);
  7.  
  8. for($i = $c; $i>=0 ; $i--){
  9.  
  10.   echo $i.'-'.$data->transfer[$i]->time.'<br>';
  11.  
  12. }
  13. ?>
  14.  
which results in giving the the output as follows

6-
5-1251866501
4-1251866308
3-1251804653
2-1251802528
1-1251802455
0-1251802411


Did i followed the correct way?

or is there an anyother formatted approach for this one?

And can i make a search and display results in xml file like doing in sql...

if there give me some ideas..
Sep 2 '09 #5
Dormilich
8,651 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. // last index is usually 1 lower than length
  2. for($i = $c-1; $i>=0 ; $i--){
And can i make a search and display results in xml file like doing in sql...
you can use XPath to search if you want to stay inside XML or use PHP to look inside the XML string, depending on the search requirements.
Sep 2 '09 #6
vjayis
134 100+
ya thats what i m asking.,

how can search inside xml file using php without using any loops.

any examples..
Sep 2 '09 #7
Dormilich
8,651 Expert Mod 8TB
@vjayis
what do you need loops for?
Sep 2 '09 #8
vjayis
134 100+
k fine if i need to display items with date='2009-07-11' from the above example means how can i do that..
Sep 2 '09 #9
Dormilich
8,651 Expert Mod 8TB
guess I’d use XPath (you could also do a text search, but it would be more complicated to get the items…)

XPath (get all items with a given date)
Expand|Select|Wrap|Line Numbers
  1. //transfer[date="_date_"]
Sep 2 '09 #10
vjayis
134 100+
sorry i had not used Xpath and i dont know how to implement it into my code..

could you guide me..
Sep 2 '09 #11
Dormilich
8,651 Expert Mod 8TB
XPath in SimpleXML: xpath()

XPath Tutorial

XPath is a convenient way to address XML nodes. if you’re familiar with the DOM (e.g. from using Javascript) this shouldn’t be too complicated.
Sep 2 '09 #12
vjayis
134 100+
thanks i ll gothrough it..

(you could also do a text search, but it would be more complicated to get the items…)
how can i do a text search using php..
Sep 2 '09 #13
Dormilich
8,651 Expert Mod 8TB
@vjayis
Expand|Select|Wrap|Line Numbers
  1. $xmltxt = file_get_contents("test.xml");
  2. // or whichever search function you need
  3. $pos = strpos($xmltxt, $_date_);
the point is, you need some clever code (probably including RegEx) to get the items from that (the XML is treated as a string, not as XML)
Sep 2 '09 #14
vjayis
134 100+
thanks

and i had worked an search from an example
Expand|Select|Wrap|Line Numbers
  1. $filename='example.xml';
  2.  
  3. $data = simplexml_load_file($filename);
  4. $op = $data->xpath('/entries/entry[time>1251802411]');
  5.  
  6.    foreach($op as $ops) {
  7.     echo $ops->acno."-".$ops->time."<br />";
  8.    }
  9.  
which outputs

B900326-1251802455
B900326-1251802528
B900326-1251804653
B900326-1251866308
B900326-1251866501

in this how can i search two or more conditions for example.....

acno='B900326' & time>'1251802411'

how to implement it
Sep 2 '09 #15
Dormilich
8,651 Expert Mod 8TB
@vjayis
add another square bracket.

Expand|Select|Wrap|Line Numbers
  1. node[condition1][condition2]
Sep 2 '09 #16
vjayis
134 100+
ohh yeah thanks yar.,

guess its going to be very easy than sql search....

thanks
Sep 2 '09 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

15 posts views Thread by Ridimz | last post: by
5 posts views Thread by Stanley Yue | last post: by
5 posts views Thread by Julia | last post: by
2 posts views Thread by krukinews | last post: by
1 post views Thread by MikeY | last post: by
1 post views Thread by vkrasner | last post: by
5 posts views Thread by Sumana | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.