473,396 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Read xml file from last entry

134 100+

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...

Sep 2 '09 #1
16 4285
8,658 Expert Mod 8TB
you can read XML in PHP either by DOMDocument or SimpleXML
Sep 2 '09 #2
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
8,658 Expert Mod 8TB
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
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>
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';
  4. $data = simplexml_load_file($filename);
  6. $c=count($data->transfer);
  8. for($i = $c; $i>=0 ; $i--){
  10.   echo $i.'-'.$data->transfer[$i]->time.'<br>';
  12. }
  13. ?>
which results in giving the the output as follows


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
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
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
8,658 Expert Mod 8TB
what do you need loops for?
Sep 2 '09 #8
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
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
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
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
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
8,658 Expert Mod 8TB
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
134 100+

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


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

acno='B900326' & time>'1251802411'

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

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

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

Sep 2 '09 #17

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

Similar topics

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
by: Stanley Yue | last post by:
I made a map like this: typedef struct { char * Source; bool DirFlag; } RegInfo_t; struct Comp {
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 log entire objects) I am going to use ...
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 let's say last 24 hours, one solution is to read...
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 it with a new name. My problem arises when the...
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!!) string sMyvalue = ConfigurationManager.AppSettings; ...
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 and writing the sector beyond 6GB Disk or...
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 interested in. Doing this is really easy using SAX....
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 changed data to a new file. However my importing...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.