I am working on a script for judges of a film festival to review and vote for films. I was thinking I could minimize the project by saving all the results in a single XML file. My concern however is if multiple judges are casting their vote at the same time, will there be a conflict with the XML file being written to at the same time?
Here is my thought on a schema :
<festival>
<teams>
<team id='*'>
<name></name>
<video>http://vimeo.com/####</video>
<ratings>
<judge id='%'>(1-7)</judge>
</ratings>
<nominations>
<judge id='%'>#</judge>
<nominations>
</team>
</teams>
<awards>
<award id='#'>Best Director</award>
</awards>
<judges>
<judge id='%'>
<name></name>
<email></email>
<password></password>
<lastVideoWatched></lastvideowatched>
</judge>
</judges>
Okay first of all,
Why are you using xml files? It would be much easier to use a database for this sort of thing. Even mysqli will work quite well. You can use simple xml to parse the file and save it in memory before committing, but I see no way of doing it concurrently. But I cannot see anyway of doing concurrent transactions without building an engine in the middle.
IF you'll take my advice, switch to mysql
Related
I am having trouble finding a solution to a problem I am facing, parsing XMLs.
Let me describe what I have now and what's the issue:
I have LINKs of XMLs files that have for example:
<prodcuts>
..
<product>
<id>1</id>
<name><![CDATA[ this is a test product name ]]></name>
<link><![CDATA[http://www.google.com]]></link>
<image><![CDATA[http://www.google.com/image.jpg]]></image>
<sku><![CDATA[ ]]></sku>
<category><![CDATA[ System > Technology ]]></category>
<price>20</price>
<description><![CDATA[ ]]></description>
<instock><![CDATA[ Y ]]></instock>
<availability>Y</availability>
</product>
..
</products>
Another XML has:
<prodcuts>
..
<product>
<productID>1</productID>
<title><![CDATA[ ]]></title>
<link><![CDATA[http://www.google.com]]></link>
<image><![CDATA[http://www.google.com/image.jpg]]></image>
<sku><![CDATA[ ]]></sku>
<categoryPath><![CDATA[ System > Technology ]]></categoryPath>
<price>20</price>
<description><![CDATA[ ]]></description>
<instock><![CDATA[ Y ]]></instock>
<availability>Y</availability>
<size>40</size>
</product>
..
</products>
Now, the difference between those are
1) the first one has a tag name "name", the other one has a tag name "title".
2) The second one has some tags that the first one does not.
Now the problem is, I am parsing the XML file via PHP like this:
$xml->products->product[$i]->id
$xml->products->product[$i]->name
and so on.. If I do this the code I have wrote, will work only for the first one. The tags that are missing is not a problem for now, cause I am inserting to Database NULL cause there are not required fields..
But, what about the second XML? Can I do something "automatically" in order to avoid asking to correct those tags?
This could be done only manually, by grabbing the content of this LINK (via PHP) and rename those ones?
I do not have the file from my clients, just the LINK of XML.
thanks in advance!
ok! I believe I have found some solutions to my problem.. I wrote them here in case someone has the same issues:
Solutions:
i) Read all the children of XML file, no matter how they are written (case-sensitive) and add them to Database. After that, there is a dashboard/PHP file with SQL queries that MATCH those children elements tags of XML with the one that you want.
In this case, you may want to create a file called whatever you like, for example test.xml and CREATE the one that you want, with the correct XML tag elements. In this case, you could UPDATE this, every some hour (according to your needs) via a cronjob..
ii) Create manually the PHP file with the parsing inside, for every XML that you get. Just make sure to keep the XML link in your DB
iii) Ask the client to give you the correct XML. XML is case-sensitive for a reason.
In case you choose the first solution you need to make changes to php.ini file too, cause the XML files may be too large and the max_execution_time is probably too low to run all these PHP - MySQL scripts.
if someone need more explain or have any better advice, please share!
I'm working on a PHP script using SimpleXML / XPath that needs to print citations for sentences from an XML file which has structure similar to the following:
<text name="text_title">
<book name="book_title">
<chapter name="chapter_title">
<sentence name="sentence_number" id="0000">
<word attr="desired_val" id="1111" />
<word attr="undesired_val" id="2222" />
</sentence>
</chapter>
</book>
</text>
The issue is that I need to return each sentence containing a word bearing attr="desired_val", and then a citation containing its text, book, chapter, and sentence number. I'm currently doing the first part with the xpath query
//word[#$attr='desired_val']/ancestor::sentence
and the second part with a series of subsequent xpath queries based on the ID attribute of each returned sentence, e.g. for the text node:
/text/[book/chapter/sentence[#id={$id}]]/#name
(and so on, for the other relevant nodes). My issue is that this becomes grossly inefficient with large numbers of records, and is causing the script to timeout with more than about ten results. Can anyone suggest ideas about a better way to do this?
If you need all matches, the only optimization I can imagine is to reduce the enormous amount of queries. It takes much time to build the whole list of matches, in order to seek for each match into the document to collect the remaining information. Instead it would be better to query the necessary data from you document in just one step. The same problem occurs in database applications, where people execute too many SQL statements instead of doing everything in just one query.
The SQL for XML is called XQuery. If you use XQuery instead of XPath you can collect all the necessary data in just one step. The following example has been tested with Saxon-HE as a XQuery engine.
<results>
{
for $x in doc("text.xml")/text/book/chapter/sentence/word
where $x/#attr = "desired_val"
return <match text="{$x/../../../../#name}"
book="{$x/../../../#name}"
chapter="{$x/../../#name}"
sentence="{$x/../#name}" />
}
</results>
The following command
java -cp /usr/share/java/Saxon-HE.jar net.sf.saxon.Query '!indent=yes' text.xquery
extracts the required information from the document in just one step.
<?xml version="1.0" encoding="UTF-8"?>
<results>
<match chapter="chapter_title"
text="text_title"
book="book_title"
sentence="sentence_number"/>
</results>
Saxon-HE can be installed on Ubuntu by the following command.
apt-get install libsaxonhe-java
I do not know which XQuery engine is best suited for PHP.
I have a simple PHP application, which uses MySQL DB, but I think that maybe the using of DB is needlessly for such easy operations.
Anyway, I hove some problems with the XML operations.
Let's say I want to have XML structure like this:
<root>
<experiment>
<name>test</name>
<accessCount>5</accessCount>
<downloadEntry>
<date>2015-11-27</date>
<comment>comment</comment>
</downloadEntry>
<downloadEntry>
<date>2015-11-28</date>
<comment>comment</comment>
</downloadEntry>
</experiment>
</root>
Now I would like to know, how to do these operations:
Count download entries (count of downloadEntry nodes) of experiment with name "test". Via XPATH?
Get download entries of experimetn with name test - but I would like to have pagination on this. So get download entries somehow like LIMIT 0,5.
The biggest problem is that, when there are no experiments - so the XML is , the loading of XML with simplexml_load_file fails. I can't open it. Yes, I can add the condition - if the XML is empty, donť open it. But I need to write to it and can't write if it isnť open.
Is there a solution for that?
Thanks everyone
I'm looking into the possibility of efficiently comparing two similar XML-files and updating outdated information.
The main XML-file I'm working with is about 200-250mb in size. The second is a tad smaller.
The two XML-files pretty much looks like this:
<product>
<Category>BOOK</Category>
<Bookgroup>BOOKF</Bookgroup>
<Productname>Name of the book</Productname>
<Productcode>123456789</Productcode>
<Price>79.00</Price>
<Availability>Stock On Order</Availability>
<ProductURL>www.url.com</ProductURL>
<Release>07.08.2013</Release>
<Author>Name of author</Author>
<Genre>Crime</Genre>
<BookType>Pocket</BookType>
<Language>English</Language>
</product>
As you can see I'm working with books, and the purpose of having a second XML-file with the same information is that I only want one copy of each book for further use.
Basically I'm trying to figure out how I effectively can parse through the first XML and check whether the book exists in the second XML. If it exists I'll check if productinformation (price, availabilty etc) have been updated. If this information has been updated this needs to be updated in the second XML as well.
If it doesnt exist it needs to be added to the second XML.
Using XMLReader I'm able to parse through each book from the first XML fairly fast (40ish seconds to loop through 4,5million lines of XML and echo out all the books) by using a similar approach as this.
My problem occurs when I want to check if this book exists in the second XML and make changes in the second XML if it needs to be updated or added.
Would it for example be possible to use XMLReader on the second XML and stop at nodes with the same booktitle as I've stopped at in the first XML and then make the check? If so how?
thanks in advance for those who will lend their time answering this question.
I’m trying to display two external XML data into my page. Let us say the fictional location of these XMLs are www.ExampleDomain1.com/xml-1.xml and www.ExampleDomain2.com/xml-2.xml respectively. The two XMLs have different element tags but have common contents, here's the example:
XML-1.xml
<property>
<type>SP</type>
<subtype>Apartment</subtype>
<refno>011248</refno>
<title>Fantastic Facilities!</title>
<description> Offering this fantastic 2 bedroom apartment set within this popular building in The Views. The property offers in excess of 1450sqft of internal living space comprising of two double bedrooms (en-suite to master), fitted kitchen with integrated appliances, main bathroom and spacious lounge/diner leading onto a good size balcony with partial views of the golf course and views of the Marina skyline.
</description>
<size>1458</size>
<sizeunits>SqFt</sizeunits>
<price>1525000</price>
<pricecurrency>AED</pricecurrency>
<totalclosingfee>1525000</totalclosingfee>
<bedrooms>2</bedrooms>
<bathrooms>2</bathrooms>
<locationtext>The Views</locationtext>
<locationlat>25.090200</locationlat>
<locationlon>55.170200</locationlon>
<developer>0</developer>
<lastupdated>2011-02-18 20:15:08</lastupdated>
<photos>
<photo>
http://www.ExampleDomain1.com/images/med_imgga94jqe351494b66b20eaec11fe501f5bdf797f4.jpg
</photo>
<photo>
http://www. ExampleDomain1.com/images/med_imgga94l1maf3ccdd27d2000e3f9255a7e3e2c48800.jpg
</photo>
</photos>
</property>
XML-2.xml
<listings>
<category>SP</category>
<subcategory>Apartment</ subcategory >
<reference>011250</reference>
<title>Fantastic Facilities!</title>
<description> A fantastic 1 bedroom apartment in the exclusive Downtown area. The property offers 850 sq.ft. of internal living space. Fantastic layout and personalized design. Externally the property has an easy accessible carport with parking for one.
</description>
<size>1200</size>
<unitsize>SqFt</ unitsize >
<price>905000</price>
<currency>AED</currency>
<closingfee>1525000</closingfee>
<bedrooms>2</bedrooms>
<bathrooms>2</bathrooms>
<location> Downtown </location>
<locationlon>55.170200</locationlon>
<developer>0</developer>
<updated>2011-02-18 20:15:08</updated>
<photos>
<photo> http://www.ExampleDomain2.com/images/med_imgga94jqe351494b66b20eaec11fe501f5bdf797f4.jpg
</photo>
<photo> http://www. ExampleDomain2.com/images/med_imgga94l1maf3ccdd27d2000e3f9255a7e3e2c48800.jpg
</photo>
</photos>
</listings>
From the above example you can clearly see that the XML tags varied differently though both have same contents. Someone will say why not clean the XML before fetching it? The problem is both XMLs were produced by proprietary software which do not have an option to manipulate the output. So matching the element tags first is not viable besides it will be a huge task for me if I will do it manually especially if the data are big.
To make things more complicated, I want to fetch both XMLs and display both results together in just one page. I want to load both XMLs simultaneously and display it in a search result.
I’ve been burning my eyes for a week now and the closest code example I’ve found was this http://net.tutsplus.com/tutorials/javascript-ajax/use-jquery-to-retrieve-data-from-an-xml-file/. The problem with this code example is that it loads only one XML from local directory. What if I want to load many XMLs at once from external source?
To elaborate more what exactly I want to achieve are these:
Make a search form that will get both XMLs and display it in just one page.
Make the image appear as the code example from the tutorial above is different from the XML structure that I have.
What application that I must know to accomplish this case (PHP or JQuery or AJAX or combination of three?).
Can it be achieved even if the XMLs would not be stored in a database?
As a newbie in coding PHP and just following some Jquery script examples, without your help the above problem will took me ages before I could find out the right solution. I’m confident in HTML and CSS but not in programming side.
Can you please help me show the path that I need to follow on (example codes)? Or is there someone Genius out there that could throw me the exact SCIENTFIC codes that I’m looking for?
Many thanks,
Mike
Ok. THAT seems to be quite complicated. But actually you can achieve this with a medium amount of work.
I will not provide you with any code or examples right now but show the possibilities how you can solve that problem.
First things first. You can do it with Javascript/jQuery only. This would mean that the client has lots of work and fetching to do, but it is possible.
You can also do it with a combination of PHP an JS. This would mean that the server would do the heavy work to fetch and match the data and send a combined result back to the client.
If it is always the same date the PHP solution has a major advantage. You could cache the combined data so you could save time matching the XML every time.
Just think about what will work better for you and we can help you coding for stuff. (Just to get clear: The XML always looks like the examples you provided? The fields/tags in the XML are always the same? If not the entire thing is much more complicated.)