Jump to content

Query Xml


shadowayex

Recommended Posts

I'm learning to work with XML as a storage option for a web application I'm building. I chose XML, because unlike databases, for my purpose I can allow my XML "records" have a dynmic ammount of data (i.e. one root tag can have 4 children, the next can have 2, and so on). I know that, technically, valid XML has to have a DTD that prevents that effect, but for my purposes it's not important to include a DTD to limit it. Anyway, on to my actual question.I'm learning to parse my XML using the PHP XML Expat tutorial. I was wondering if there was a querying system that would allow me to parse only certain data. Something along the lines of what PHP MySQL can do. I want myself and a couple other people to be able to use this application to add whatever data, edit whatever data, and read whatever data.The reason that I cannot use databasing effectively for this particular application is because, as I mention, two "records" that are categorized in the same place may have a different amount of data. A good example is one part of this application helps us keep track of small sales we make. One customer could buy one item, another could buy 6, and so on. It'd be hard to make a structured database to keep track of it all efficiently. It could be done, yes, but I'd have to make more databse tables and write the application to read through each table and find all the relating data to one transaction when (if Expat can) I can kep all the data from a transaction in one "record" and have PHP just search for that particular transaction. Make sense?Can it be done?

Link to comment
Share on other sites

Personally, I don't like Expath. I prefer libxml's XMLReader. If you need to support PHP4 though, using Expath is basically a must. I certainly don't need to support it.XML files are traditionally best read when in a dozen. In other words, designate a folder in which the file name will be the transaction's ID or whatever, and parse just that. Assuming the transaction isn't of many different items (under 2000 or so), you could even use DOM, which will make things even easier when it comes to editing.If you still want to keep it all in one file, be warned that edits will make the application slower as the file grows. To edit a large XML file (that would be uneditable with DOM due to DOM's high memory usage), you need to use something like XMLReader to read it, and something like XMLWriter to write a copy of it, expect in the case where the node is to be edited, in which case you write the new value. After that, you replace the original with the copy (by using rename()). Going over all nodes, and writing them out take some time after all.Bottom line is that neither Expath nor XMLReader can write and edit XML files. They just read them. XMLWriter can only write files, but not read or edit existing ones. Only DOM and SimpleXML can read, write and edit files efficiently enough, and they have a high memory consumption, which however is unnoticeable on smaller files.

Link to comment
Share on other sites

Alright, that makes sense. So if I use SimpleXML, kept each separate transaction and whatnot in separate XML files, this would work fine?
Basically, yes. BTW, I'd reccomend DOM instead of SimpleXML (mostly due to its richer capabilities), but that's your call.I also want to add something to your comment about DTD:
I chose XML, because unlike databases, for my purpose I can allow my XML "records" have a dynmic ammount of data (i.e. one root tag can have 4 children, the next can have 2, and so on). I know that, technically, valid XML has to have a DTD that prevents that effect, but for my purposes it's not important to include a DTD to limit it.
DTD, as well as all XML validation languages (XML Schema, RelaxNG, Schematron) can declare optional elements. Those languages are for validation (e.g. Is a required element present? Does it have the right kind of value?), not for "limitation" (though the validation languages do have some limitations, therefore limiting your XML expressiveness). If you want your language to have optional elements, you can do that.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...