PDA

View Full Version : Reading content in XML on server



toplisek
03-14-2006, 05:49 PM
I would like to read files XML. Do you know how to do?:)

Twey
03-14-2006, 05:58 PM
Read, or parse? Reading is done the same as any other file:
<?php
$xml = implode(file("myfile.xml"), "\n");
?>There is an extension (http://www.php.net/xml) to PHP to parse XML, unless disabled at build time.

toplisek
03-14-2006, 06:08 PM
thanks,
it works showing and reading files on web site . Do you know if files have structure content, how to store files in table or there is usually procedure for such files that I can publish e.g. last 10 up-to-date?

Actually I receive files ending HTML.:)

Twey
03-14-2006, 06:34 PM
Do you know if files have structure content, how to store files in table or there is usually procedure for such files that I can publish e.g. last 10 up-to-date?I don't understand you. You want to store the files themselves in a table of some sort? This can be done using a binary field (LONGBLOB is a good choice if using MySQL).
Actually I receive files ending HTML.Eh?

toplisek
03-14-2006, 06:52 PM
You want to store the files themselves in a table of some sort?

I do not know any other possibility to sort such files by date.Or maybe...


LONGBLOB is a good choice if using MySQL

Can you please explain me more?

I have files in HTML and its structure is defined. So, position of date, title of article and full contant is defined. The name of files is changing.

I would like to publish just e.g. first 5 articles with
link:
Date
Headline

User will click on link and article will be shown in full content

xerxes
03-15-2006, 10:07 PM
I'd have to see the XML, but you could certainly use the DOM XML. It's usually built in to PHP4. In PHP5 the equivalent PHP4 DOM is a PECL package, so you just have to add the extension, although the DOM in PHP5 (that is built in) is better.
Then just do a loop for each one of the items and dynamically create the table information (including the article, but the article will have a display:none). Then using JS DOM, make it so that when you click on the link it'll set display:block. If you don't like that, then do a popup window or goto another page (i.e. _self).

PHP4 php.net page: manual/en/ref.domxml.php
PHP5: manual/en/ref.dom.php

The reason for incomplete URLs is for server reasons (mirrors, languages, countries, etc.)

Other alternatives include SimpleXML (PHP5), XML Parser (expat include), and many others, including 'homemade' classes.

Twey
03-16-2006, 05:36 PM
The reason for incomplete URLs is for server reasons (mirrors, languages, countries, etc.)If you link to the URL on www.php.net it'll select and redirect to a mirror of its choosing. Also, it should be noted:
Requirements

This extension makes use of the GNOME XML library. Download and install this library. You will need at least libxml-2.4.14. To use DOM XSLT features you can use the libxslt library and EXSLT enhancements from http://www.exslt.org/. Download and install these libraries if you plan to use (enhanced) XSLT features. You will need at least libxslt-1.0.18.
Then just do a loop for each one of the items and dynamically create the table information (including the article, but the article will have a display:none).Warning: may cause your page to load like a pig.

xerxes
03-17-2006, 03:18 AM
Warning: may cause your page to load like a pig.
Ok, I know that I do not know everything, so how would you go about this? I know that if you have an immense amount of data, it'll load like a pig. But how would you go about it? Load just the headers? Or use something like AJAX and constantly load on more data (which would kill the connection end, or at the least take it down to a crawl)?

toplisek
03-17-2006, 08:20 AM
Then just do a loop for each one of the items

- Can you tell me if XML files can as extension HTML or allways XML? I'm new.
- What do you mean by loop?

Is correct code for this:


<?php

if (!$dom = domxml_open_file("example.xml")) {
echo "Error while parsing the document\n";
exit;
}

$root = $dom->document_element();
?>


- What to do if names of files are changing?

xerxes
03-17-2006, 02:34 PM
- Can you tell me if XML files can as extension HTML or allways XML? I'm new.
- What do you mean by loop?

-The XML file can have any extension.
-The loop is something like:


for ($i = 0; $i < $items->length; $i++) {
echo $items->item($i)->nodeValue . "\n";
}



Is correct code for this:
Close enough. I like the below for better parsing.


if (!$dom = domxml_open_file("suggestions.xml",DOMXML_LOAD_PARSING +//0
DOMXML_LOAD_COMPLETE_ATTRS + //8
DOMXML_LOAD_SUBSTITUTE_ENTITIES + //4
DOMXML_LOAD_DONT_KEEP_BLANKS //16
)) {
echo "Error while parsing the document\n";
exit;
}
$doc = $dom->document_element();



- What to do if names of files are changing?
Use the directory functions to look for the appropriate filename structure in a specified directory.

Twey
03-17-2006, 04:28 PM
- Can you tell me if XML files can as extension HTML or allways XML? I'm new. Any file can have any extension. Extensions are only a convention to make the contents of the file more easily guessable at a glance, and don't have any bearing on the actual content of the file.

toplisek
03-17-2006, 06:24 PM
-The XML file can have any extension.
-The loop is something like:


for ($i = 0; $i < $items->length; $i++)
{
echo $items->item($i)->nodeValue . "\n";
}


What will do actually this code?




Use the directory functions to look for the appropriate filename structure in a specified directory.

Can you tell specific function to use directory function? Need help...:)

Twey
03-17-2006, 06:43 PM
What will do actually this code?Absolutely nothing, as the variable "items" isn't defined. It's an example of code that loops through all the elements inside a given element (or is that an array of elements? I don't know much about the PHP implementation of XMLDOM) and echoes the values of all tags inside it.

toplisek
03-17-2006, 07:01 PM
Thank you for the start,:)