|
[ hr_programer @ 12.09.2008. 07:01 ] @
| Znači imam kod kojim kreiram XML, a sad bih trebao pročitati RSS i spremiti u polje naravno i onda ide kreiranje XML-a.
Kako pročitati RSS, čuo sam da to treba preko XML parsera!?
Molim za upute i usmjerenja tako da od nekud počnem mada mislim da nebi trebalo biti puno problema s tim!? |
[ hr_programer @ 12.09.2008. 09:19 ] @
Ovo sam našao na net-u, to kao služi za parsanje!
Kako i kud dalje?
Code:
// Create an XML parser
$xml_parser = xml_parser_create();
// Set the functions to handle opening and closing tags
xml_set_element_handler($xml_parser, "startElement", "endElement");
// Set the function to handle blocks of character data
xml_set_character_data_handler($xml_parser, "characterData");
// Open the XML file for reading
$fp = fopen("http://www.adriamare.hr/gb_adriamare.xml","r")
or die("Error reading RSS data.");
// Read the XML file 4KB at a time
while ($data = fread($fp, 4096))
// Parse each 4KB chunk with the XML parser created above
xml_parse($xml_parser, $data, feof($fp))
// Handle errors in parsing
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
// Close the XML file
fclose($fp);
// Free up memory used by the XML parser
xml_parser_free($xml_parser);
[ stankons @ 12.09.2008. 09:54 ] @
Mozes da koristis SimplePIE, ja sam licno koristio MagPIE, ali mislim da se ne razvija vise, zadnji build je bio 2005.
Ovo je primer za MagPIE:
Code:
<?php
header('Content-Type: text/html; charset=utf-8');
require_once('rss_fetch.inc');
define("MAGPIE_OUTPUT_ENCODING", "UTF-8");
$url ='http://www.promet.si/Feeds/?id=5';
$rss = fetch_rss( $url );
echo "<li>";
echo '<a href="'.($rss->items[0]["link"]).'">'.($rss->items[0]["title"]);
echo ($rss->items[0]["description"]).': </a>';
echo "</li>";
?>
Naravno prvo skines MagPie i otpakujes ga u folder gde ti nalazi kod.
HTH's
[ hr_programer @ 12.09.2008. 10:10 ] @
Ovo je kod koji ispiše samo title, link i description!
Code:
<html>
<head>
<title> Current SitePoint Articles </title>
</head>
<body>
<dl>
<?php
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
$tag = $name;
} elseif ($name == "ITEM") {
$insideitem = true;
}
}
function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link;
if ($name == "ITEM") {
printf("<dt><b><a href='%s'>%s</a></b></dt>",
trim($link),htmlspecialchars(trim($title)));
printf("<dd>%s</dd>",htmlspecialchars(trim($description)));
$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}
function characterData($parser, $data) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "DESCRIPTION":
$description .= $data;
break;
case "LINK":
$link .= $data;
break;
}
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://www.neka_stranica.com/test.xml","r")
or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
?>
</dl>
</body>
</html>
Ja trebam pročitati jedan RSS koji ima još osim ovih svoje neke elemente u svkom item-u.
Kako da ih pročitam stavim u polje i zapišem u drugi XML?
[Ovu poruku je menjao hr_programer dana 12.09.2008. u 11:24 GMT+1]
[Ovu poruku je menjao hr_programer dana 12.09.2008. u 11:25 GMT+1]
[ hr_programer @ 12.09.2008. 10:17 ] @
Ovaj xml koji treba pročitati je ovakvog sadržaja:
Code:
<item>
<title>Naslov oglasa</title>
<description>Ovdje ide opis </description>
<link>http://www.moja_stranica.com/link_na_taj_oglas</link>
<g:area>58 square m</g:area>
<g:publish_date>2008-02-03</g:publish_date>
<g:expiration_date>2008-12-04</g:expiration_date>
<g:price>69000</g:price>
<g:id>0093ANDNOV</g:id>
<g:image_link>http://www.moja stranica.com/slika.jpg</g:image_link>
<g:property_type>apartment</g:property_type>
<g:region type="string">Naziv regije</g:region>
<g:country type="string">Croatia</g:country>
<g:listing_type>for sale</g:listing_type>
<c:city_village type="string">Novigrad</c:city_village>
<c:postal_code type="int">23000</c:postal_code>
<c:price_in_euro type="string">69000 euro</c:price_in_euro>
<c:living_space type="string">58 sq m</c:living_space>
<c:continent type="string">europe</c:continent>
<c:country type="string">croatia</c:country>
<c:agent_phone type="string">+385 neki broj telefona</c:agent_phone>
<c:agent_phone type="string">+385 neki broj telefona</c:agent_phone>
</item>
Naravno, više je takvih oglasa u toj stranici, ali ovo je jedan primjer!
[ stankons @ 12.09.2008. 10:43 ] @
Ovo ti je primer za garea:
Code:
<html>
<head>
<title> Current SitePoint Articles </title>
</head>
<body>
<h2>::Adria Mare:: Croatia Real Estate</h2>
<dl>
<?php
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$garea = "";
function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $description, $link, $garea;
if ($insideitem) {
$tag = $name;
} elseif ($name == "ITEM") {
$insideitem = true;
}
}
function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link, $garea;
if ($name == "ITEM") {
printf("<dt><b><a href='%s'>%s</a></b></dt>",
trim($link),htmlspecialchars(trim($title)));
printf("<dd>%s</dd>",htmlspecialchars(trim($description)));
printf("<dd>%s</dd>",htmlspecialchars(trim($garea)));
$title = "";
$description = "";
$link = "";
$insideitem = false;
$garea = "";
}
}
function characterData($parser, $data) {
global $insideitem, $tag, $title, $description, $link, $garea;
if ($insideitem) {
switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "DESCRIPTION":
$description .= $data;
break;
case "LINK":
$link .= $data;
break;
case "G:AREA":
$garea .= $data;
break;
}
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://www.adriamare.hr/gb_adriamare.xml","r")
or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
?>
</dl>
</body>
</html>
Pripazi na case "G:AREA" mora biti velikim slovima.
HTH's
[ hr_programer @ 12.09.2008. 10:55 ] @
U ovoj funkciji
Code:
function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link, $garea;
if ($name == "ITEM") {
printf("<dt><b><a href='%s'>%s</a></b></dt>",
trim($link),htmlspecialchars(trim($title)));
printf("<dd>%s</dd>",htmlspecialchars(trim($description)));
$title = "";
$description = "";
$link = "";
$garea = "";
$insideitem = false;
}
}
ispišem G:AREA!
A kako bi ga stavio u polje da mogu kreirati drugi XML?
Tj. kako ga staviti u polje i prebaciti u drugu funkciju!
Probao sam u ovoj funkciji staviti $polje[] = $garea;
i prebaciti u neku svoju funkciju i onda tamo izborjati elemente da provjerim da je to to ali nije ok!
Jel ti možda znaš?
[Ovu poruku je menjao hr_programer dana 12.09.2008. u 12:10 GMT+1]
[ hr_programer @ 12.09.2008. 11:51 ] @
Probao sam samo u ovoj funkciji kreirat taj novi xml, uzeo sam za primjer samo id oglasa, i kreira ga, ali naravno sve id-ove strpa u taj jedan element propert-id!
Code:
function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link, $id_oglasa;
// Creates an instance of the DOMImplementation class
$imp = new DOMImplementation;
// Creates a DOMDocument instance
############## DTD VALIDATION change tho code down with this $pc = $imp->createDocument("", "", $dtd);
$pc = $imp->createDocument("", "");
$pc->encoding = 'UTF-8';
$pc->standalone = false;
// create the root element named PROPERTIES
$properties = $pc->createElement('properties');
// dodaj PROPERTIES element kao child DOMdokumentu
$pc->appendChild($properties);
if($bufer!=$id_oglasa){
$property = $pc->createElement('property');
$properties->appendChild($property);
$property_id= $pc->createElement('property-id');
$property->appendChild($property_id);
$property_id->appendChild($pc->createTextNode($id_oglasa));
}
$bufer = $id_oglasa;
//save DATA as XML_Result
$xml_result = $pc->saveXML();
// save DATA as propertycroatia.xml
$pc->save("test.xml");
}
zato bi trebao strpat to u polje, izbrojat elemente i te informacije onda prebacit u drugu funkciju koja bi u for petlji kreirala taj propertzy-id sve dok ima id oglasa, tj. ponovo bi kreirala element property-id i njemu vrijednost a to je id oglasa! Može pomoč?
[ hr_programer @ 12.09.2008. 13:03 ] @
Samo kad bi mi netko pokazao kako kreirati ovaj property element onoliko puta koliko ima $id_oglasa. 99% sam siguran da bi znao dalje!
Anybody?
[ Man-Wolf @ 13.09.2008. 04:09 ] @
Probaj da konsultujes GOOGLE: Google: "PHP How to read RSS and prepare it for XML"
On obicno da najvise i najbrze odgovore :-))
Copyright (C) 2001-2025 by www.elitesecurity.org. All rights reserved.
|