Jump to content

Need some help with xml domdocument


Z33M@N

Recommended Posts

Hi have written a script that runs through the xml document and gets the data of the requested nodes. then it stores the nodes in a mysql table.

 

Now when I'm running the script I'm also looking for a attribute called "name" in a node called "PICTURE".

 

The script runs and pulls all the xml files out of the specified directory (keep in mind all of the xml files has the same structure) and then uses the domdocument to read the nodes in the files.

 

but when it comes to a xml document that does not have the "PICTURE" Node it starts tot get the following error: Fatal error: Call to a member function getAttribute() on a non-object in

 

Is there a way to keep the script running even if there isn't a "PICTURE" node in the file, so that it ignores that field and still get the rest of the nodes in the file.

 

THE XML WITHOUT IMAGE:

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="#!-- #EXECUTIVE:HOME_PATH --#/Folio_xsl"?>

<DOCUMENT source-infobase-name="A_Sae" content-collection-id="A_Sae" source-infobase-title="A_Sae" base-output-dir="F:\Projects\MigrationXML\Learning\XML\A_Sae_Output\" default-split-level="Alv" makefile-filename="F:\Projects\MigrationXML\Learning\XML\A_Sae_Output\A_Sae.mak" css-stylesheet-href="#!-- #STYLESHEETS:Stylesheet.CSS --#" xml-output-dir="F:\Projects\MigrationXML\Learning\XML\A_Sae_Output\XML\" makefile-document-depth="3">
<LEVEL style-name="8lv" style-name-escaped="lv" style-id="0-0-0-13" level-depth="3" toc-section="false">
	<RECORD id="0-0-0-44" number="49" version="3">
		<HEADING>Politieke arena</HEADING>
		<PARA style-name="pBold-Blue-11" style-name-escaped="pBold-Blue-11" style-id="0-0-0-37">Politieke arena</PARA>
	</RECORD>
	<LEVEL style-name="Normal Level" style-name-escaped="Normal-Level" style-id="0-0-0-0" level-depth="0" toc-section="false">
		<RECORD id="0-0-0-47" number="50" version="84">
			<PARA>Die klem op die bewaring van die eie (etniese of ander groepswaardes, taal, kultuur) het ook deurgesypel na die politieke arena, waar etniese konflik op byna elke vasteland tot sulke mate intensiveer het dat ingryping deur die internasionale gemeenskap telkens nodig geword het om verreikende skade weens die rimpeleffek in die globale arena te beperk. Hierdie ingryping neem die vorm aan van byvoorbeeld internasionale afgesante/bemiddelaars en vredesbewaringsendings deur die lidstate van die Verenigde Nasies (VN).</PARA>
		</RECORD>
		<RECORD id="0-0-0-250354" number="51" version="84">
			<PARA>Dit geld in die een of ander vorm vir onder meer Soedan (Darfoer-streek) in Afrika en die Balkan in Europa, die Israelies-Palestynse konflik in die Midde-Ooste en internasionale ingrepe om Suid-Amerika uit sy ekonomiese moeras te help lig. In Europa het dit in sommige gevalle aanleiding gegee tot die herrysing van die ver-regsgesindes en diskriminasie teen immigrante uit arm lande, terwyl waarskuwingsliggies geflikker het toe jong immigrante in Parys in 2005 amok gesaai het oor hul uitsluiting van die Franse gemeenskap.</PARA>
		</RECORD>
		<RECORD id="0-0-0-250355" number="52" version="84">
			<PARA>Die ekonomiese opgang van China en Indië, die heropkoms van Rusland en die sterker samehang van die Latyns-Amerikaanse blok het in 2007 die VSA se supermagstatus verder bedreig nadat dit klaar geknou is deur 'n plaaslike resessie. Amerika se globale politieke status is verder aangetas deur sy voortslepende militêre teenwoordigheid in Irak wat onstabiliteit in die Midde-Ooste eerder aangewakker het as om demokrasie en vrede te bring. Die Obama-administrasie, wat in 2008 die mag in die VSA oorgeneem het, het vroeë tekens getoon dat hulle die situasie aanspreek, met hoëvlak-onderhandelinge met Israel en Hamas nadat 'n brose skietstilstand weer in die slag gebly het.</PARA>
		</RECORD>
		<RECORD id="0-0-0-250356" number="53" version="84">
			<PARA>Obama, wat die Nobelprys vir Vrede in 2009 ontvang het vir die "audacity of hope" (uitdagendheid van hoop) wat hy na die globale politieke verhoog gebring het, het ironies genoeg in sy aanvaardingtoespraak te kenne gegee dat 'n oorlogvrye wêreld nie altyd moontlik is nie. Dit is geïnterpreteer as 'n verwysing na die VSA se voortdurende teenwoordigheid in die Midde-Oosterse lande Irak en Afganistan, en sy vrese omtrent 'n kernbedreiging vanaf Iran. Obama het wel die VSA se verbintenis tot die inkrimping van sy kernarsenaal herhaal en aangekondig dat hy afsien van die "Star Wars"-ruimteskild teen kernwapens.</PARA>
		</RECORD>
	</LEVEL>
</LEVEL>
</DOCUMENT>

 

XML WITH IMAGE:

 

<DOCUMENT source-infobase-name="E_Sae" content-collection-id="E_Sae" source-infobase-title="South African Encyclopedia" base-output-dir="F:\Projects\MigrationXML\Learning\XML\E_Sae_Output\" default-split-level="Alv" makefile-filename="F:\Projects\MigrationXML\Learning\XML\E_Sae_Output\E_Sae.mak" css-stylesheet-href="#!-- #STYLESHEETS:Stylesheet.CSS --#" xml-output-dir="F:\Projects\MigrationXML\Learning\XML\E_Sae_Output\XML\" makefile-document-depth="1">
<LEVEL style-name="6lv" style-name-escaped="lv" style-id="0-0-0-7" level-depth="1" toc-section="false">
	<RECORD id="0-0-0-1" number="1" version="3">
		<HEADING>Contents</HEADING>
		<PARA style-name="pBold-Orange-12-Verd" style-name-escaped="pBold-Orange-12-Verd" style-id="0-0-0-43">
			<PARAFORMAT tab-count="1">
				<TABSTOP number="0" position="285" justification="left" leader="none"/>
				<[b]PICTURE[/b] id="0-0-0-123-img" object-class="folio objects" name="icon_sae.gif" href="#!-- #EXECUTIVE:HOME_PATH --#/img/icon_sae.gif" width="858" height="735" left-border-line-color="0" left-border-line-width="0" left-border-space="0" right-border-line-color="0" right-border-line-width="0" right-border-space="0" top-border-line-color="0" top-border-line-width="0" top-border-space="0" bottom-border-line-color="0" bottom-border-line-width="0" bottom-border-space="0"/>&#160;  <DESTINATION id="0-0-0-125" name="E_Sae_Title"/>South African Encyclopedia<CHARSTYLE style-name="cBold-Orange-12-Verd" style-name-escaped="cBold-Orange-12-Verd" style-id="0-0-0-78"> </CHARSTYLE>
				<CHARSTYLE style-name="cHidden" style-name-escaped="cHidden" style-id="0-0-0-82">- Contents</CHARSTYLE>
			</PARAFORMAT>
		</PARA>
	</RECORD>
	<LEVEL style-name="Normal Level" style-name-escaped="Normal-Level" style-id="0-0-0-0" level-depth="0" toc-section="false">
		<RECORD id="0-0-0-2" number="2" version="3">
			<PARA style-name="pBold-Blue-bgLBlue-11" style-name-escaped="pBold-Blue-bgLBlue-11" style-id="0-0-0-39">Contents</PARA>
		</RECORD>
		<RECORD id="0-0-0-3" number="3" version="3">
			<PARA>The South African Encyclopedia has been compiled from a South African perspective and with the needs of a new generation of South Africans in mind. To access the information:</PARA>
		</RECORD>
		<RECORD id="0-0-0-4" number="4" version="3">
			<PARA style-name="pInd0.2-1.0-T1.0" style-name-escaped="pInd0-2-1-0-T1-0" style-id="0-0-0-57">
				<CHARSTYLE style-name="cWingdings" style-name-escaped="cWingdings" style-id="0-0-0-90">Ø</CHARSTYLE>
				<TAB tab-count="1"/>Click on the categories below to expand the subcategories, then the sections (sometimes even subsections) and lastly open the articles. </PARA>
		</RECORD>
		<RECORD id="0-0-0-5" number="5" version="3">
			<PARA style-name="pInd0.2-1.0-T1.0" style-name-escaped="pInd0-2-1-0-T1-0" style-id="0-0-0-57">
				<CHARSTYLE style-name="cWingdings" style-name-escaped="cWingdings" style-id="0-0-0-90">Ø</CHARSTYLE>
				<TAB tab-count="1"/>The articles are also listed alphabetically in the Table of Contents on the left-hand side of your screen. Right-click with your mouse on "Contents" and "Expand all" to view the full alphabetical list.</PARA>
		</RECORD>
		<RECORD id="0-0-0-6" number="6" version="3">
			<PARA style-name="pInd0.2-1.0-T1.0" style-name-escaped="pInd0-2-1-0-T1-0" style-id="0-0-0-57">
				<CHARSTYLE style-name="cWingdings" style-name-escaped="cWingdings" style-id="0-0-0-90">Ø</CHARSTYLE>
				<TAB tab-count="1"/>Click on the article of your choice to go directly to its contents, or scroll down until you reach the desired article.<LINEBRK/>
			</PARA>
		</RECORD>
		<RECORD id="0-0-0-7" number="7" version="3">
			<PARA>
				<TABLE>
					<TABLEFORMAT number-of-widths="2" horizontal-gap="72" left-indent="912" left-border-space="43" left-border-width="15" left-border-color="#C0C0C0" right-border-space="43" right-border-width="15" right-border-color="#C0C0C0" top-border-space="43" top-border-width="15" top-border-color="#C0C0C0" bottom-border-space="43" bottom-border-width="15" bottom-border-color="#C0C0C0">
						<COLUMNFORMAT column-width="51" percentage="no"/>
						<COLUMNFORMAT column-width="232" percentage="no"/>
					</TABLEFORMAT>
					<ROW>
						<COL>
							<PARA style-name="pCenter" style-name-escaped="pCenter" style-id="0-0-0-52">
								<CELLFORMAT bottom-border-space="0" bottom-border-width="15" bottom-border-color="#C0C0C0">
									<CELL cells-merged-right="1">
										<CHARSTYLE style-name="cBold-Orange-11" style-name-escaped="cBold-Orange-11" style-id="0-0-0-77">Table of Contents</CHARSTYLE>
									</CELL>
								</CELLFORMAT>
							</PARA>
						</COL>
					</ROW>
					<ROW>
						<COL>
							<PARA>
								<CELLFORMAT>
								</CELLFORMAT>
							</PARA>
						</COL>
						<COL>
							<PARA style-name="pInd0.2-1.0-T1.0" style-name-escaped="pInd0-2-1-0-T1-0" style-id="0-0-0-57">
								<CELLFORMAT>
									<CHARSTYLE style-name="cWingdings" style-name-escaped="cWingdings" style-id="0-0-0-90">Ø</CHARSTYLE>
									<TAB tab-count="1"/>
									<CHARSTYLE style-name="cBold" style-name-escaped="cBold" style-id="0-0-0-69">
										<LINK style-name="Jump" style-name-escaped="Jump" style-id="0-0-0-94" type="Jump" destination-name="E_Sae_Foreword" destination-id="0-0-0-127">Foreword</LINK>
									</CHARSTYLE>
								</CELLFORMAT>
							</PARA>
							<PARA style-name="pInd0.2-1.0-T1.0" style-name-escaped="pInd0-2-1-0-T1-0" style-id="0-0-0-57">
								<CHARSTYLE style-name="cWingdings" style-name-escaped="cWingdings" style-id="0-0-0-90">Ø</CHARSTYLE>
								<TAB tab-count="1"/>
								<CHARSTYLE style-name="cBold" style-name-escaped="cBold" style-id="0-0-0-69">
									<LINK style-name="Jump" style-name-escaped="Jump" style-id="0-0-0-94" type="Jump" destination-name="E_Sae_EditorMessage" destination-id="0-0-0-129">Editor's message</LINK>
								</CHARSTYLE>
							</PARA>
							<PARA style-name="pInd0.2-1.0-T1.0" style-name-escaped="pInd0-2-1-0-T1-0" style-id="0-0-0-57">
								<CHARSTYLE style-name="cWingdings" style-name-escaped="cWingdings" style-id="0-0-0-90">Ø</CHARSTYLE>
								<TAB tab-count="1"/>
								<CHARSTYLE style-name="cBold" style-name-escaped="cBold" style-id="0-0-0-69">
									<LINK style-name="Jump" style-name-escaped="Jump" style-id="0-0-0-94" type="Jump" destination-name="E_Sae_Contributors" destination-id="0-0-0-131">Contributors</LINK>
								</CHARSTYLE>
							</PARA>
						</COL>
					</ROW>
					<ROW>
						<COL>
							<PARA style-name="pCenter" style-name-escaped="pCenter" style-id="0-0-0-52">
								<CELLFORMAT>
									<PICTURE id="0-0-0-133-img" object-class="Folio Objects" name="Sci_Logo01z.jpg" href="#!-- #EXECUTIVE:HOME_PATH --#/img/Sci_Logo01z.jpg" width="573" height="465" left-border-line-color="0" left-border-line-width="0" left-border-space="0" right-border-line-color="0" right-border-line-width="0" right-border-space="0" top-border-line-color="0" top-border-line-width="0" top-border-space="0" bottom-border-line-color="0" bottom-border-line-width="0" bottom-border-space="0"/>
								</CELLFORMAT>
							</PARA>
						</COL>
						<COL>
							<PARA>
								<CELLFORMAT alignment="middle">
									<CHARSTYLE style-name="cBold" style-name-escaped="cBold" style-id="0-0-0-69">
										<LINK style-name="Jump" style-name-escaped="Jump" style-id="0-0-0-94" type="Jump" destination-name="E_Sae_ScienceE" destination-id="0-0-0-135">Science and Technology</LINK>
									</CHARSTYLE>
								</CELLFORMAT>
							</PARA>
						</COL>
					</ROW>
					<ROW>
						<COL>
							<PARA style-name="pCenter" style-name-escaped="pCenter" style-id="0-0-0-52">
								<CHARSTYLE style-name="cBold" style-name-escaped="cBold" style-id="0-0-0-69">
									<CELLFORMAT>
									</CELLFORMAT>
								</CHARSTYLE>
								<PICTURE id="0-0-0-137-img" object-class="Folio Objects" name="Soc_Logo01z.jpg" href="#!-- #EXECUTIVE:HOME_PATH --#/img/Soc_Logo01z.jpg" width="573" height="403" left-border-line-color="0" left-border-line-width="0" left-border-space="0" right-border-line-color="0" right-border-line-width="0" right-border-space="0" top-border-line-color="0" top-border-line-width="0" top-border-space="0" bottom-border-line-color="0" bottom-border-line-width="0" bottom-border-space="0"/>
							</PARA>
						</COL>
						<COL>
							<PARA>
								<CELLFORMAT alignment="middle">
									<CHARSTYLE style-name="cBold" style-name-escaped="cBold" style-id="0-0-0-69">
										<LINK style-name="Jump" style-name-escaped="Jump" style-id="0-0-0-94" type="Jump" destination-name="E_Sae_SocietyE" destination-id="0-0-0-139">Society and the Humanities</LINK>
									</CHARSTYLE>
								</CELLFORMAT>
							</PARA>
						</COL>
					</ROW>
					<ROW>
						<COL>
							<PARA style-name="pCenter" style-name-escaped="pCenter" style-id="0-0-0-52">
								<CHARSTYLE style-name="cBold" style-name-escaped="cBold" style-id="0-0-0-69">
									<CELLFORMAT>
									</CELLFORMAT>
								</CHARSTYLE>
								<PICTURE id="0-0-0-141-img" object-class="folio objects" name="projecttheme.gif" href="#!-- #EXECUTIVE:HOME_PATH --#/img/projecttheme.gif" width="858" height="597" left-border-line-color="0" left-border-line-width="0" left-border-space="0" right-border-line-color="0" right-border-line-width="0" right-border-space="0" top-border-line-color="0" top-border-line-width="0" top-border-space="0" bottom-border-line-color="0" bottom-border-line-width="0" bottom-border-space="0"/>
							</PARA>
						</COL>
						<COL>
							<PARA>
								<CELLFORMAT alignment="middle">
									<CHARSTYLE style-name="cBold" style-name-escaped="cBold" style-id="0-0-0-69">
										<LINK style-name="Jump" style-name-escaped="Jump" style-id="0-0-0-94" type="Jump" destination-name="E_Sae_ProjectFramework" destination-id="0-0-0-143">Project Framework</LINK>
									</CHARSTYLE>
								</CELLFORMAT>
							</PARA>
						</COL>
					</ROW>
				</TABLE>
			</PARA>
		</RECORD>
		<RECORD id="0-0-0-8" number="8" version="3">
			<PARA style-name="pLine-Purple" style-name-escaped="pLine-Purple" style-id="0-0-0-65">
			</PARA>
		</RECORD>
	</LEVEL>
</LEVEL>
</DOCUMENT>

 

THE PHP CODE:

 

<?php

function get_links() {

// connect to database
$con = mysql_connect("localhost","root","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("xml_import_db", $con);

/**
*Get all XML files in the directory.
*/

$string="";
$fileCount=0;
//$filePath=$PATH.'/home/holla22/public_html/superimtips.com/xml/xml/english/'; # Specify the path you want to look in. 
$filePath=$PATH.'\xampp\htdocs\sae\xml\afrikaans'; # The path of the directory

$dir = opendir($filePath); # Open the path
while ($file = readdir($dir)) { 
  if (eregi("\.xml",$file)) { # Look for files with a .xml extension
    $string = "$file";

//$string[$fileCount] = $string;
    $fileCount++;

echo $string;


$resUrl = new DOMDocument();
$resUrl->load('xml/afrikaans/'.$string);

//$prnt0 = 'entry';

$xmlResults = $resUrl->getElementsByTagName("DOCUMENT");
//if ($xmlResults->length<>0) {
foreach($xmlResults as $value){



  $pictures = $value->getElementsByTagName( "PICTURE" ); # IMAGES
  $picture = $pictures->item(0)->getAttribute('name');
  
  
  $headings = $value->getElementsByTagName( "HEADING" ); # SUB HEADINGS
  $subheading = $headings->item(1)->nodeValue;
  
  $paras = $value->getElementsByTagName( "PARA" ); # MAIN HEADINGS
  $mainheading = $paras->item(0)->nodeValue;
  
  $levels = $value->getElementsByTagName( "LEVEL" ); # MAIN BODY TEXT level or
  $body = $levels->item(0)->nodeValue;
  }
  //$records = $value->getElementsByTagName( "RECORD" ); # IMAGES
  //$record = $records->item(0)->nodeValue;

  
print_r("<p>".utf8_decode($picture)."</p>");

print_r("<p>".utf8_decode($mainheading)."</p>");

print_r("<p>".utf8_decode($subheading)."</p>");

//print_r("<p>".utf8_decode($record)."</p>");

print_r(utf8_decode("<p>".$body."</p>"));

  
//escape and sanitize...
  $picture = mysql_real_escape_string($picture); # IMAGES
  $body = mysql_real_escape_string($body); # MAIN BODY TEXT
  $mainheading = mysql_real_escape_string($mainheading); # MAIN HEADINGS
  $subheading = mysql_real_escape_string($subheading); # SUB HEADINGS
  
  
  
  //insert into database
//$q = "INSERT INTO articles_a1(articlexml, articletitle, articlesubheading, articlebody, articleimages) values ('$string', '$mainheading', '$subheading', '$body', '$picture')";
//$rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());

}
  }
  mysql_close(); #close connection

echo "<h2>Data Imported Successfully</h2>";
  
  
}
get_links();
?>

Link to comment
Share on other sites

  $pictures = $value->getElementsByTagName( "PICTURE" ); # IMAGES

if ($pictures){

  $picture = $pictures->item(0)->getAttribute('name');

}

 

maybe?

 

Hi,

 

I tried something simular and tested yours still getting the error: Fatal error: Call to a member function getAttribute() on a non-object in

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.