472,328 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 4172
Dormilich
8,658 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,658 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,658 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,658 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,658 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,658 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,658 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,658 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

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

Similar topics

15
by: Ridimz | last post by:
When use ifstream, how do I ignore the last line of a file if it doesn't contain any information? Thanks in advance, Ridimz
5
by: Stanley Yue | last post by:
I made a map like this: typedef struct { char * Source; bool DirFlag; } RegInfo_t; struct Comp {
5
by: Julia | last post by:
Strategy to read part of XML log file Hi, My log file will contains entries formatted as XML(i use this format since some time i would like to...
2
by: krukinews | last post by:
Hi, I'm trying to write small program that reads events from computer and stores them in SQL database. But I don't know hove to read logs for...
1
by: MikeY | last post by:
Hopefully someone can help, I have a listview box where I display my desired files. I single click on the desired file to be renamed and I rename...
1
by: vkrasner | last post by:
It works with VS2003 and does not in VS2005: in VS2003 : string sMyvalue = ConfigurationSettings.AppSettings; in VS2005 (does not work!!)...
5
by: Sumana | last post by:
Hi All, We developed our project on VC++.Net console application to create image of disk and to write the image We are having problem with reading...
8
by: kepioo | last post by:
I currently have an xml input file containing lots of data. My objectiv is to write a script that reports in another xml file only the data I am...
2
by: Brad King | last post by:
Hello Everyone, I am having trouble reading a file. My eventual plan is to read in the file, fix a formatting problem with a regex and export the...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.