Parsing feed that's inside namespace - php

I have tried several different approaches here, but to no avail. Can someone explain to me how the following type of feed can be parsed? Below is an excerpt
<?xml version='1.0'?>
<NoticeResults xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<sql:query>
<Notice>
<PersonId>174171199</PersonId>
<NamePrefix></NamePrefix>
<NameAdditionalPrefix></NameAdditionalPrefix>
<FirstName>Donna</FirstName>
<MiddleName></MiddleName>
<LastName>Autrey</LastName>
<NameSuffix></NameSuffix>
<NameAdditionalSuffix></NameAdditionalSuffix>
<MaidenName></MaidenName>
<City></City>
<State>IL</State>
<Country>United States</Country>
<DateEntered>2015-02-17T00:00:00</DateEntered>
<DateCompleted>2015-02-17T00:00:00</DateCompleted>
<DateExpired>2015-03-19T00:00:00</DateExpired>
<NoticeText><p><B>PEKIN </B>- Donna Lou (Morris) Autrey, 83, of Pekin passed away at 1:10 p.m. Sunday, Feb. 15, 2015, at Autumn Accolade in Green Valley.</p><p>A graveside service will be at 11 a.m. Thursday at Lakeside Cemetery. There will be no visitation. Preston-Hanley Funeral Homes &amp; Crematory is in charge of arrangements.</p>
</NoticeText>
<NoticeType>Courtesy</NoticeType>
<Status>Active</Status>
<FromToYears></FromToYears>
<AffiliateSite>PJStar</AffiliateSite>
<AffiliateAdId>Autrey_02/17/2015_4673625</AffiliateAdId>
<PublishedBy>Peoria Journal Star</PublishedBy>
<DisplayURL>http://www.legacy.com/Link.asp?I=LS000174171199</DisplayURL>
<LocationList></LocationList>
<ShowInSpotlight>0</ShowInSpotlight>
<DateCreated>2015-02-17T01:12:37.510</DateCreated>
<RowVersion>793618506</RowVersion>
<GuestBookURL></GuestBookURL>
</Notice>
<Notice>
<PersonId>174171209</PersonId>
<NamePrefix></NamePrefix>
<NameAdditionalPrefix></NameAdditionalPrefix>
<FirstName>Lois</FirstName>
<MiddleName></MiddleName>
<LastName>Barden</LastName>
<NameSuffix></NameSuffix>
<NameAdditionalSuffix></NameAdditionalSuffix>
<MaidenName></MaidenName>
<City>Peoria</City>
<State>IL</State>
<Country>United States</Country>
<DateEntered>2015-02-17T00:00:00</DateEntered>
<DateCompleted>2015-02-17T00:00:00</DateCompleted>
<DateExpired>2015-03-19T00:00:00</DateExpired>
<NoticeText><img src="/Images/Cobrands/PJStar/Photos/C7KC6NVUW02_021715.jpg" align="left" border="0" vspace="4" hspace="10" lgyOrigName="C7KC6NVUW02.jpg"><p><B>PEORIA </B>- Lois Mae Barden, age 84, of Peoria passed away Sunday, Feb. 15, 2015, at OSF Saint Francis Medical Center in Peoria.</p><p>Lois was born on Sept. 12, 1930, in Macomb, Ill. She married James W. Barden on June 12, 1948, in Peoria. He preceded her in death in June of 1988. </p><p>She also was preceded in death by her parents and 12 siblings.</p><p>Surviving are her children, Kathy (Ted) Kindred of Manito, Ill., Lynn (Allen) Simer of Nineveh, Ind., and Jim (Deb) Barden of North Pekin, Ill.; four grandchildren; six great-grandchildren; one great-great-grandson; and her two sisters, Doris Butts and Maggie Hoyt.</p><p>Lois enjoyed spending time at home with her family. She was always there to care for them when they were in need. She also enjoyed listening to Christian music, crafting, cooking and crocheting.</p><p>A visitation will be from 1 to 2 p.m. Thursday, Feb. 19, 2015, at Davison-Fulton Woodland Chapel in Peoria. Burial will follow at Swan Lake Memory Gardens in Peoria. </p><p>Memorials may be made to Illinois Cancer Care.</p><p>Online condolences may be made through <a href="http://www.davison-fulton.com" target="_new" rel="nofollow">www.davison-fulton.com</a>.</p><center><br><a rel="nofollow" href="http://www.davison-fulton.com" target="_blank"><img src="/Images/Cobrands/PJStar/Logos/www.davison-fulton.com.jpg" border="0"></a></center>
</NoticeText>
<NoticeType>Paid</NoticeType>
<Status>Active</Status>
<FromToYears></FromToYears>
<AffiliateSite>PJStar</AffiliateSite>
<AffiliateAdId>Barden_02/17/2015_102257420</AffiliateAdId>
<PublishedBy>Peoria Journal Star</PublishedBy>
<DisplayURL>http://www.legacy.com/Link.asp?I=LS000174171209</DisplayURL>
<LocationList></LocationList>
<FHIndex>10868</FHIndex>
<FHName>Davison-Fulton Woodland Chapel</FHName>
<FHKnownByName1>Davison-Fulton Woodland Chapel</FHKnownByName1>
<FHAddressLine1>2021 North University Street</FHAddressLine1>
<FHCity>Peoria</FHCity>
<FHStateProvince>IL </FHStateProvince>
<FHZipCode>61604 </FHZipCode>
<FHPhoneNumber1>3096885700 </FHPhoneNumber1>
<FHUrl>www.davison-fulton.com</FHUrl>
<ShowInSpotlight>0</ShowInSpotlight>
<DateCreated>2015-02-17T01:13:01.857</DateCreated>
<ImageUrl>http://mi-cache.legacy.com/legacy/images/Cobrands/PJStar/Photos/C7KC6NVUW02_021715.jpg</ImageUrl>
<RowVersion>793622447</RowVersion>
<GuestBookURL>http://www.legacy.com/Link.asp?I=GB000174171209</GuestBookURL>
</Notice>
</sql:query>
</NoticeResults>
I have been banging my head against the table on this all day (huge waste of 8 hours). All we have to work with in our offices are PHP (limitation by our CMS). Can someone please explain how I would go about parsing through a feed like this? I've gotten pretty far, but still can't seem to figure this out. Please also explain why you do whatever you do in order to make this work. I'll have to parse several feeds structured this way and i've never dealt with this before. Any help will be greatly appreciated.
Thanks!
Chris

You can access the elements using DOM Extension. There is an example of looping through XML from w3schools.
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("doc.xml");
$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item) {
// Do whatever is needed
}
?>

Related

xpath get text and anchors between br tags

I have a, for me, very hard question:
this is the HTML I'm dealing with:
<td>
Margaret (Parky) DeVogelaere
(19 June
2011 -
16 August
2019) (his death)
<br/>
Portia Rebecca "Becky" Crockett
(11 November
1975 -
2011) (divorced)
<br/>
Susan Brewer (8 October
1961 -
15 April
1974) (divorced)
(2 children)
</td>
How do i get with xpath everything, including anchor between the start of td and first br/ and repeat that for everything between the first br/ and the next one? and so on.
I hope i make myself clear and I'm not a professional, just a hobby programmer
NodeValue just gives all of the text but not the href from a possible person anchor, witch is not always the case as you can see
So to make it more clear this is what i want:
Margaret (Parky) DeVogelaere (<a href="/date/06-19?ref_=nmbio_sp_1">19
June</a> 2011 - <a href="/date/08-16?ref_=nmbio_sp_1">16
August</a> 2019) (his death)
Portia Rebecca "Becky" Crockett
(11 November
1975 -
2011) (divorced)
Susan Brewer (8 October
1961 -
15 April
1974) (divorced)
(2 children)
This comes from imdb https://www.imdb.com/name/nm0001228/bio Family, spouse part
I can get the td in question, but don't understand how i get the data that i want
$cells = $xp->query("//table[contains(#id, 'tableFamily')]/tr[1]/td[2]")
Or does anybody know a different approach?
Well, your html is a litlle funky, so one way to get what you want (or close to it) is to perform some surgery on the original HTML (replacing the <br/>s with </td><td>s so as to create parsable elements, and then use an HTML parser and xpath:
$orig = '
[your html above]
';
$newstring = str_replace("<br/>", "</td><td>", $orig);
$doc = new DOMDocument();
$doc->loadHTML($newstring);
$xpath = new DOMXPath($doc);
$sources = $xpath->query('//td');
foreach ($sources as $source) {
echo $source->ownerDocument->saveHTML($source)."\r\n---------------\r\n";
};
Output:
<td>
Margaret (Parky) DeVogelaere
(19 June
2011 -
16 August
2019) (his death)
</td>
---------------
<td>
Portia Rebecca "Becky" Crockett
(11 November
1975 -
2011) (divorced)
</td>
---------------
<td>
Susan Brewer (8 October
1961 -
15 April
1974) (divorced)
(2 children)
</td>
---------------

json string is showing blank why is not getting decoded

i have json string but when i am getting it json_decode() it is showing blank.
$str = '[{"actcode":"Auck4","actname":"Sky Tower","date":"","time":"","timeduration":"","adult":"0","adultprice":"28","child":"0","childprice":"0","description":"Discover the best of Auckland in half a day. Soak up spectacular sights on this scenic tour, from heritage-listed buildings on Queen Street to the stunning Viaduct Harbour and panoramic vistas from the Sky Tower observation deck.
Start your tour with a hotel pick-up and travel through Auckland?s dynamic Central Business District. Travel across the iconic Auckland Harbour Bridge and admire stunning city views. Then, return to the city centre and visit the vibrant precinct of Wynyard Quarter. Here, wander among the sculptures and enjoy the happenings on the water of Viaduct Harbour.
Continue to Queen Street, also known as the ?Golden Mile? of Aucklands business and shopping district. Marvel at historic buildings like the Ferry Terminal building before visiting the Auckland Museum. Here, explore fascinating exhibits paying tribute to New Zealands natural, Maori and European histories. Afterwards, travel along Aucklands most expensive residential streets with fantastic views of the Waitemata Harbour and its islands.
Your tour ends at Sky Tower, the tallest free-standing structure in the Southern Hemisphere. Take in breathtaking 360-degree views of the city and its surroundings. In the afternoon, continue your own exploration of Auckland."}]';
i tried the below code
$array = json_decode($str,true);
echo print_r($array);
this one too
$str1 = trim($str);
$array = json_decode($str1,true);
echo print_r($array);
but the string si showing blank
try this one.
$string = mysql_real_escape_string($str);
$findsym = array('\r', '\n');
$removesym = array("", "");
$strdone = stripslashes(str_replace($findsym,$removesym,strip_tags($string)));
$jsonarray = json_decode($strdone,true);
echo "<pre>"; echo print_r($jsonarray);

Why is getElementsByTagName() not working for me?

I am trying to get info from the OMDb API. I found some code but it's not working right:
$loc = 'http://www.omdbapi.com/?t='.$lookup_title.'&r=xml';
$dom = new DOMDocument();
$dom->load($loc);
foreach ($dom->getElementsByTagName('movie') as $e) {
$imdb = $e->getElementsByTagName('imdbID')->item(0)->textContent;
$year = $e->getElementsByTagName('year')->item(0)->textContent;
$plot = $e->getElementsByTagName('plot')->item(0)->textContent;
$poster = $e->getElementsByTagName('poster')->item(0)->textContent;
}
The XML output from OMDb API:
<?xml version="1.0" encoding="UTF-8"?>
<root response="True">
<movie title="Terminator 2: Judgment Day"
year="1991"
rated="R"
released="03 Jul 1991"
runtime="137 min"
genre="Action, Sci-Fi"
director="James Cameron"
writer="James Cameron, William Wisher Jr."
actors="Arnold Schwarzenegger, Linda Hamilton, Edward Furlong, Robert Patrick"
plot="Almost 10 years have passed since the first cyborg called The Terminator tried to kill Sarah Connor and her unborn son, John Connor. John Connor, the future leader of the human resistance, is now a healthy young boy. However another Terminator is sent back through time called the T-1000, which is more advanced and more powerful than its predecessor. The Mission: to kill John Connor when he's still a child. However, Sarah and John do not have to face this threat of a Terminator alone. Another Terminator is also sent back through time. The mission: to protect John and Sarah Connor at all costs. The battle for tomorrow has begun..."
language="English, Spanish"
country="USA, France"
awards="Won 4 Oscars. Another 20 wins & 21 nominations."
poster="http://ia.media-imdb.com/images/M/MV5BMTg5NzUwMDU5NF5BMl5BanBnXkFtZTcwMjk2MDA4Mg##._V1_SX300.jpg"
metascore="68"
imdbRating="8.5"
imdbVotes="636,472"
imdbID="tt0103064"
type="movie"/>
</root>
Error:
Notice: Trying to get property of non-object
The imdb, year, plot, and poster parts are XML attributes, not elements, so use DOMElement::getAttribute() rather than getElementsByTagName():
$imdb = $e->getAttribute('imdbID');
$year = $e->getAttribute('year');
$plot = $e->getAttribute('plot');
$poster = $e->getAttribute('poster');

How to create csv file using raw text data

i am much confused at this point regarding the csv file creation and insert data in the database.
suppose i have below text data - that is of 45000 record set, i am posting dew of them below.
Winged Wheels in France, by Michael Myers Shoemaker 45790
A Battle Fought on Snow Shoes, by Mary Cochrane Rogers 45789
The German Classics of the Nineteenth and Twentieth Centuries, 45788
Volume 11, by Friedrich Spielhagen, Theodor Storm,
Wilhelm Raabe, Marion D. Learned and Ewald Eiserhardt
[Subtitle: Masterpieces of German Literature
Translated Into English]
Zofloya ou le Maure, Tomes 1-4, by Charlotte Dacre 45787
[Subtitle: Histoire du XVe si?cle]
[Language: French]
Their Majesties as I Knew Them, by Xavier Paoli 45786
[Subtitle: Personal Reminiscences of the
Kings and Queens of Europe]
[Translator: Alexander Teixeira de Mattos]
New York Times Current History: The European War, Vol. 8, 45785
Pt. 2, No. 1, July 1918, by Various
Gallery of Comicalities, by Robert Cruikshank, 45784
George Cruikshank and Robert Seymour
[Subtitle: Embracing Humorous Sketches]
Katri, by Emil Nervander 45783
[Subtitle: Kertomus 17 vuosi-sadasta]
[Language: Finnish]
The Little Brown Jug at Kildare, by Meredith Nicholson 45782
[Illustrator: James Montgomery Flagg]
Beaumont & Fletcher's Works (6 through 10), by Francis Beaumont 45781
and John Fletcher
[Subtitle: The Queen of Corinth; Bonduca; The Knight of the
Burning Pestle; Loves Pilgrimage; The Double Marriage]
Beaumont & Fletcher's Works (1 through 5), by Francis Beaumont 45780
and John Fletcher
[Subtitle: A Wife for a Month; The Lovers Progress;
The Pilgrim; The Captain; The Prophetess]
The Washington Historical Quarterly, Volume V, 1914, by Various 45779
[Editor: Edmond S. Meany]
Minstrelsy of the Scottish Border Volume III of 3, by Walter Scott 45778
[Subtitle: Consisting of Historical and Romantic Ballads,
Collected In the Southern Counties of Scotland; With
a Few Of Modern Date, Founded Upon Local Tradition.
In Three Volumes. Vol. III]
What i want is simply insert Winged Wheels in France, by Michael Myers Shoemaker in one column and 45790 in other column of CSV. then i will be able to add them to my database.
moreover, e.g,
The German Classics of the Nineteenth and Twentieth Centuries,
Volume 11, by Friedrich Spielhagen, Theodor Storm,
Wilhelm Raabe, Marion D. Learned and Ewald Eiserhardt
[Subtitle: Masterpieces of German Literature
Translated Into English]
i want to insert above text in this way:
The German Classics of the Nineteenth and Twentieth Centuries,
Volume 11, by Friedrich Spielhagen, Theodor Storm,
Wilhelm Raabe, Marion D. Learned and Ewald Eiserhardt
means no this portion:
[Subtitle: Masterpieces of German Literature
Translated Into English]
the ", by" should also omitted and so my new data would be like this. so actually i need three columns in the csv.
1 | Winged Wheels in France | Michael Myers Shoemaker | 45790
2 | The German Classics of the Nineteenth and Twentieth Centuries,
Volume 11 | Friedrich Spielhagen, Theodor Storm,
Wilhelm Raabe, Marion D. Learned and Ewald Eiserhardt | 45789
Please help in getting it inserted in excel file and create csv from it.
thank you all.
do like this
(not tested)
$f = file_get_contents('yourtextfile.txt');
$f = preg_replace('/\[(.*?)\]/s','',$f);
$f = str_replace(array("\n", "\r"), '', $f);
file_put_contents('temp.txt',$f);
$file = file('temp.txt');
foreach($file as $key => $line){
if($line!=null || $line!='')
{
mysqli_query($connection,"insert into table1(column1) values('$line')");
}
}
edit
$f = file_get_contents('tt.txt');
$f = preg_replace('/\[(.*?)\]/s','',$f);
$keywords = preg_split("/[ ]{15}/", $f);
print_r(array_filter($keywords));
i'm still not clear,whether you have those numbers in your file or you have just mentioned it there!!

PHP, Memory and Iteration

I've been noticing some odd behavior while experimenting with benchmarking SplFixedArrays. Take this little snippet of code, for instance...
<?php
$splFixedArray = new \SplFixedArray( 100000 );
echo number_format( memory_get_usage() ) . PHP_EOL;
$variable = 'Truffaut single-origin coffee wayfarers, church-key asymmetrical 90\'s trust fund hashtag before they sold out thundercats photo booth. Godard sustainable roof party keffiyeh, Odd Future chillwave mlkshk kogi VHS leggings hoodie art party next level dreamcatcher yr. Blog american apparel aesthetic tattooed farm-to-table, stumptown viral whatever mixtape raw denim Williamsburg skateboard flexitarian actually tofu. Echo Park lomo disrupt PBR, jean shorts irony fingerstache blog kale chips. Street art iPhone PBR fingerstache Bushwick Cosby sweater. McSweeney\'s mumblecore semiotics, twee quinoa tofu +1 fingerstache pop-up. Echo Park bitters disrupt irony. Truffaut single-origin coffee wayfarers, church-key asymmetrical 90\'s trust fund hashtag before they sold out thundercats photo booth. Godard sustainable roof party keffiyeh, Odd Future chillwave mlkshk kogi VHS leggings hoodie art party next level dreamcatcher yr. Blog american apparel aesthetic tattooed farm-to-table, stumptown viral whatever mixtape raw denim Williamsburg skateboard flexitarian actually tofu. Echo Park lomo disrupt PBR, jean shorts irony fingerstache blog kale chips. Street art iPhone PBR fingerstache Bushwick Cosby sweater.';
var_dump( $variable );
for( $i = 0; $i < 100000; $i++ )
{
$splFixedArray[ $i ] = $variable;
}
echo number_format( memory_get_usage() );
Which outputs...
1,032,080
string(1209) "Truffaut single-origin coffee wayfarers, church-key asymmetrical 90's trust fund hashtag before they sold out thundercats photo booth. Godard sustainable roof party keffiyeh, Odd Future chillwave mlkshk kogi VHS leggings hoodie art party next level dreamcatcher yr. Blog american apparel aesthetic tattooed farm-to-table, stumptown viral whatever mixtape raw denim Williamsburg skateboard flexitarian actually tofu. Echo Park lomo disrupt PBR, jean shorts irony fingerstache blog kale chips. Street art iPhone PB"...
1,032,384
Now, let's add a simple random integer onto the end while in the for loop...
<?php
$splFixedArray = new \SplFixedArray( 100000 );
echo number_format( memory_get_usage() ) . PHP_EOL;
$variable = 'Truffaut single-origin coffee wayfarers, church-key asymmetrical 90\'s trust fund hashtag before they sold out thundercats photo booth. Godard sustainable roof party keffiyeh, Odd Future chillwave mlkshk kogi VHS leggings hoodie art party next level dreamcatcher yr. Blog american apparel aesthetic tattooed farm-to-table, stumptown viral whatever mixtape raw denim Williamsburg skateboard flexitarian actually tofu. Echo Park lomo disrupt PBR, jean shorts irony fingerstache blog kale chips. Street art iPhone PBR fingerstache Bushwick Cosby sweater. McSweeney\'s mumblecore semiotics, twee quinoa tofu +1 fingerstache pop-up. Echo Park bitters disrupt irony. Truffaut single-origin coffee wayfarers, church-key asymmetrical 90\'s trust fund hashtag before they sold out thundercats photo booth. Godard sustainable roof party keffiyeh, Odd Future chillwave mlkshk kogi VHS leggings hoodie art party next level dreamcatcher yr. Blog american apparel aesthetic tattooed farm-to-table, stumptown viral whatever mixtape raw denim Williamsburg skateboard flexitarian actually tofu. Echo Park lomo disrupt PBR, jean shorts irony fingerstache blog kale chips. Street art iPhone PBR fingerstache Bushwick Cosby sweater.';
var_dump( $variable );
for( $i = 0; $i < 100000; $i++ )
{
$splFixedArray[ $i ] = $variable . rand();
}
echo number_format( memory_get_usage() );
Which results in this...
1,034,320
string(1209) "Truffaut single-origin coffee wayfarers, church-key asymmetrical 90's trust fund hashtag before they sold out thundercats photo booth. Godard sustainable roof party keffiyeh, Odd Future chillwave mlkshk kogi VHS leggings hoodie art party next level dreamcatcher yr. Blog american apparel aesthetic tattooed farm-to-table, stumptown viral whatever mixtape raw denim Williamsburg skateboard flexitarian actually tofu. Echo Park lomo disrupt PBR, jean shorts irony fingerstache blog kale chips. Street art iPhone PB"...
129,834,272
What I'm curious about is why function calls are resulting in stacked memory usage. Is it normal that memory would not be freed up after the iteration?
This behavior is expected.
In the first case, you are storing the same value multiple times, which can be implemented as a single instance of the value and a batch of references to it, with a copy-on-write semantic for cases when the value at a given array index is changed.
In the second case, you are storing many different values, which can't be handled the same way; memory must be allocated for the full contents of each value, which results in the difference you see in memory consumption between the two cases.

Categories