Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2002
    Location
    Bohol, Philippines
    Posts
    72

    Extracting data from XML file by PHP

    hi everybody,

    I have a problem reading this xml file that i assigned to $marc variable:
    Code:
      <datafield tag="650" ind1=" " ind2="0">
        <subfield code="a">Nursing care plans.</subfield>
      </datafield>
      <datafield tag="650" ind1=" " ind2="0">
        <subfield code="a">Hospitals</subfield>
        <subfield code="x">Case management services.</subfield>
      </datafield>
      <datafield tag="541" ind1=" " ind2=" ">
        <subfield code="a">MegaTEXTS</subfield>
        <subfield code="c">purchased</subfield>
        <subfield code="d">9/30/11</subfield>
      </datafield>
      <datafield tag="906" ind1=" " ind2=" ">
        <subfield code="a">7</subfield>
        <subfield code="b">cbc</subfield>
        <subfield code="c">orignew</subfield>
        <subfield code="d">1</subfield>
      </datafield>
    I was able to get the values of tag='650' using this:

    Code:
    	$obj = new SimpleXMLElement($marc);
    	
    	foreach ($obj->datafield as $datafield) {
    		if ($datafield['tag'] == '650') {
    			$subject=$subject.$datafield->subfield."<br>";
    		}
    	}
    My problem is how to extract the data from a specific 'subfield'. For example, how can i extract '9/30/11' from datafield tag='541' under subfield code='d'? Take note, there is also a subfield code='d' from datafield tag='906'.

    Thank you.

  2. #2
    Join Date
    Apr 2005
    Location
    Cochin
    Posts
    2,452
    You should probably use XML parser class
    http://php.net/manual/en/book.xml.php

  3. #3
    Join Date
    Sep 2002
    Location
    Bohol, Philippines
    Posts
    72
    thanks for the link.

    but i think my problem is that i lack the looping inside every datafield. that i'm not able to get.

  4. #4
    Join Date
    Apr 2005
    Location
    Cochin
    Posts
    2,452

  5. #5
    Join Date
    Sep 2002
    Location
    Bohol, Philippines
    Posts
    72
    thank you very much for the links.i got the solution from what you provided.

    here's my code:
    Code:
    $obj = new SimpleXMLElement($marc);
    	
    foreach ($obj->datafield as $datafield) {
        if ($datafield['tag'] == '541') {
    	foreach ($datafield->subfield as $subfield) {
               if ($subfield['code'] == 'd') {
    	      $this->acquisition=$subfield;
    	   }
    	}
        }
    }
    The code simply gets the value of datafield tag='541' with subfield code='d'. the result is '9/30/11'.

Similar Threads

  1. PHP, reading from an xml file
    By Shib in forum Programming Discussion
    Replies: 2
    Last Post: 06-21-2010, 03:03 PM
  2. [PHP] Extracting data between two characters - a lot harder than I thought!
    By jonathanbull in forum Programming Discussion
    Replies: 6
    Last Post: 11-19-2006, 02:02 AM
  3. Extracting MYSQL table/field data into a text file?
    By UkWebsiteFan in forum Programming Discussion
    Replies: 1
    Last Post: 11-09-2004, 03:34 AM
  4. Extracting data using PHP [Template Sys]
    By nick[x1] in forum Programming Discussion
    Replies: 6
    Last Post: 08-16-2004, 09:29 AM
  5. Extracting raw log file data
    By brookie in forum Web Hosting
    Replies: 0
    Last Post: 09-21-2001, 11:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •