Reading XML into PHP - php

I'm trying to determine the best course of action for the display of data for a project I'm working on. My client is currently using a proprietary CMS geared towards managing real estate. It easily lets you add properties, square footage, price, location, etc. The company that runs this CMS provides the data in a pretty straightforward XML file that they say offers access to all of the data my client enters.
I've read up on PHP5's SimpleXML feature and I grasp the basic concepts well enough, but my question is: can I access the XML data in a similar fashion as if I were querying a MySQL database?
For instance, assuming each entry has a unique ID, will I be able to set up a view and display just that record using a URL variable like: http://example.com/apartment.php?id=14
Can you also display results based on values within strings? I'm thinking a form submit that returns only two bedroom properties in this case.
Sorry in advance if this is a noob question. I'd rather not build a custom CMS for my client if for no other reason than they'd only have to login to one location and update accordingly.

Some short answers on your questions:
a. Yes you can access XML data with queries, but using XPath instead of SQL. XPath is for XML what SQL is for databases, working quite different.
b. Yes you can build a php program that receives an id as parameter and uses this for an XPath search on a given XML file.
c. All data in a XML file is a string, so it is no problem to search for or display strings. Even your example id=14 is to handle as a string.
You might be interested in this further information:
http://www.ibm.com/developerworks/library/x-simplexml.html?S_TACT=105AGX06&S_CMP=LP
http://www.ibm.com/developerworks/library/x-xmlphp1.html?S_TACT=105AGX06&S_CMP=LP
PHP can access XML not only via SimpleXML but also with DOM. SimpleXML accesses the elements like PHP-arrays, DOM provides a w3c-DOM-compatible api.
See php.net for other ways to access XML, but they seem not to be appropriate for you.

Related

Search On Website using php variable - DOM Parsing

I want to search on website pragmatically using PHP like as we search on website manually, enter query on search box press search and result came out.
Suppose I want to search on this website by products names or model number that are stored in my csv file.
if the products number or model number match with website data then result page should be displayed ..
I search on below question but not able to implement.
Creating a 'robot' to fill form with some pages in
Autofill a form of another website and send it
Please let me know how we can do this PHP ..
Thanks
You want to create a “crawler” for websites.
There are some things to consider first:
You code will never be generic. Each site has proper structure and you can not assume any thing (Example: craigslist “encode” emails with a simple method)
You need to select an objective (Emails ? Items information ? Links ?)
PHP is by far one of the worst languages to do that.
I’ll suggest using C# and the library called AgilityHtmlPack. It allows you to parse HTML pages as XML documents (So you can do XPath expressions and more to retrieve information).
It surely can be done in PHP, but I think it will take at least 10x time in php compared to c#.

How to display accented characters entities in a iOS app?

In my iOS app I get data from an external PHP script which builds and returns strings using queries on a mySQL database. In this database, texts have HTML entities in them, e.g. Josè is written as
Josè
When I pass these built strings to my app, all the entities are still there but I'd like to transform them into human readable text in my app. I can't find a way to do this.
I saw questions like this one with accepted answers like this but I can't write a line for any of the hundreds of entities that exist. I mean, I could, but I can't believe there is not a way to do this in a more simple way.
Also, since I use said strings in many places from many views through all the app (text views, labels, table view cells, etc) I think it would VERY useful to apply the correct transformation in the PHP script itself, rather than in the app. So my final question is this: which is the correct way to build a string with entities in it so when I load it in my iOS app all the entities are readable characters? Thank you to ANYONE who will help me!
You can use the Google's category GTMNSString+HTML
This category possibly covers most of the HTML entities you might want to convert into human readable format.
Since I'm not able to find the Google code for this category, I'm pointing to an alternative location; MWFeedParser.
GTMNSString+HTML.h
GTMNSString+HTML.m

What is the best way to find a specified element using PHP's SimpleXMLElement class?

The project that I'm currently working on involves translating a website that was written in Python to PHP code. The part I'm doing now requires me to send $_GET requests to a web server. The response that I get back is always in XML format, so I'm using the *simplexml_load_file()* function to fetch the result.
I need to parse through the XML that the web server sends back to me. Python had a nice library called BeautifulStoneSoup, which had a find() method that returned the contents of a specified attribute.
After looking at the list of PHP functions at http://php.net/manual/en/book.simplexml.php, I cannot seem to find anything that allows me to specify an attribute name and get the contents. Keep in mind that attribute I may be looking for is not necessarily going to always be one level deep into the XML array. It may be several dimensions into the array.
I can provide an example if this wasn't specific enough. Thanks for the help, friends!
Xpath sounds like what your looking for. It allows for searches to be made within any level of an XML directory.
The following example looks multiple levels deep with an xml directory and checks if the description node contains whatever is held within $txtSearch
$holidayDoc = simplexml_load_file($url);
$qry = "//channel/item[description[contains(text(),\"$txtSearch\")]]";
$holidayDoc->xpath($qry);
Results can then be iterated through using a foreach loop
You can run an XPath query:
$xml->xpath('//#myattribute')

How can I search using PHP through an XML file and display the results?

I am trying to build a very simple price comparison script.
Until now, I wrote a code that gets some product xml feeds from shops and with the help of XSLT I create a single-global xml of all those input XMLs. I use the XSLT because the shops have different names for elements.
Now I want to take it one step further and I want to create a search form that will display me the products let's say I have the term "laptop".
I know how to create a form, but I need a coding guidance to understand how to make it to search in my XML file (products.xml) and display let's say the
Thank you
You might want to check out http://php.net/manual/en/class.xmlreader.php
Using that it is pretty easy to navigate through an XML file and grab all the info you need.
EDIT:
On second thought, http://php.net/manual/en/book.simplexml.php is a MUCH simpler way to achieve what you're trying to do. Hence the name, I guess ;)
You can use SimpleXML library to parse your xml file. In my opinion SimpleXML is easier to use than xmlreader. Though SimpleXML is introduced on php5.

How do I design a web interface for browsing text man pages?

I would like to design a web app that allows me to sort, browse, and display various attributes (e.g. title, tag, description) for a collection of man pages.
Specifically, these are R documentation files within an R package that houses a collection of data sets, maintained by several people in an SVN repository. The format of these files is .Rd, which is LaTeX-like, but different.
R has functions for converting these man pages to html or pdf, but I'd like to be able to have a web interface that allows users to click on a particular keyword, and bring up a list (and brief excerpts) for those man pages that have that keyword within the \keyword{} tag.
Also, the generated html is somewhat ugly and I'd like to be able to provide my own CSS.
One obvious option is to load all the metadata I desire into a database like MySQL and design my site to run queries and fetch the appropriate data.
I'd like to avoid that to minimize upkeep for future maintainers. The number of files is small (<500) and the amount of data is small (only a couple of hundred lines per file).
My current leaning is to have a script that pulls the desired metadata from each file into a summary JSON file and load this summary.json file in PHP, decode it, and loop through the array looking for those items that have attributes that match the current query (e.g. all docs with keyword1 AND keyword2).
I was starting in that direction with the following...
$contents=file_get_contents("summary.json");
$c=json_decode($contents,true);
foreach ($c as $ind=>$val ) { .... etc
Another idea was to write a script that would convert these .Rd files to xml. In that case, are there any lightweight frameworks that make it easy to sort and search a small collection of xml files?
I'm not sure if xQuery is overkill or if I have time to dig into it...
I think I'm suffering from too-many-options-syndrome with all the AJAX temptations. Any help is greatly appreciated.
I'm looking for a super simple solution. How might some of you out there approach this?
My approach would be parsing the keywords (from your description i assume they have a special notation to distinguish them from normal words/text) from the files and storing this data as searchindex somewhere. Does not have to be mySQL, sqlite would surely be enough for your project.
A search would then be very simple.
Parsing files could be automated as post-commit-hook to your subversion repository.
Why don't you create table SUMMARIES with column for each of summary's fields?
Then you could index that with full-text index, assigning different weight to each field.
You don't need MySQL, you can use SQLite which has the the Google's full-text indexing (FTS3) built in.

Categories