Hi guys!!!
I need some examples or an explanation for read some data of a XML file and for write some elements in the file with a form everything with PHP. Can You help me??
Thanks!!!
Printable View
Hi guys!!!
I need some examples or an explanation for read some data of a XML file and for write some elements in the file with a form everything with PHP. Can You help me??
Thanks!!!
The server that I want to put my page support PERL 5.8.7 and PHP 4.4.2.. What's the better language the read/write of/on a XML script without a DB in your opinion?? Can I choose one of them??
Reading an XML File using PHP
HTML Code:XML Code: file name - employees.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<employees>
<employee>
<name>Mark</name>
<age>27</age>
<salary>$5000</salary>
</employee>
<employee>
<name>Jack</name>
<age>25</age>
<salary>$4000</salary>
</employee>
</employees>
With the above XML file the PHP code will read the XML file and retrieve the information from it and output that in the screen.PHP Code:<?php
$doc = new DOMDocument();
$doc->load( 'employees.xml' );
$employees = $doc->getElementsByTagName( "employee" );
foreach( $employees as $employee )
{
$names = $employee->getElementsByTagName( "name" );
$name = $names->item(0)->nodeValue;
$ages= $employee->getElementsByTagName( "age" );
$age= $ages->item(0)->nodeValue;
$salaries = $employee->getElementsByTagName( "salary" );
$salary = $salaries->item(0)->nodeValue;
echo "<b>$name - $age - $salary\n</b><br>";
}
?>
Writing XML using PHP
This code will output the XML file in console as well as it will create a XML file in the name of write.xml in the same directory of the PHP script.PHP Code:<?php
$employees = array();
$employees [] = array(
'name' => 'Albert',
'age' => '34',
'salary' => "$10000"
);
$employees [] = array(
'name' => 'Claud',
'age' => '20',
'salary' => "$2000"
);
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "employees" );
$doc->appendChild( $r );
foreach( $employees as $employee )
{
$b = $doc->createElement( "employee" );
$name = $doc->createElement( "name" );
$name->appendChild(
$doc->createTextNode( $employee['name'] )
);
$b->appendChild( $name );
$age = $doc->createElement( "age" );
$age->appendChild(
$doc->createTextNode( $employee['age'] )
);
$b->appendChild( $age );
$salary = $doc->createElement( "salary" );
$salary->appendChild(
$doc->createTextNode( $employee['salary'] )
);
$b->appendChild( $salary );
$r->appendChild( $b );
}
echo $doc->saveXML();
$doc->save("write.xml")
?>
As i don't know much about perl it is not wise to comment about that. I think PHP is enough for this purpose.
But this solution works with PHP 4? or just for PHP 5?
hmmmm i've developed it with PHP 5 i don't have PHP4 could you please test that it in your machine?
Heres my conversion...
<?php
$doc = new DOMDocument();
$doc->load( 'info.xml' );
$blog = $doc->getElementsByTagName( "post" );
foreach( $blog as $post )
{
$headers = $post->getElementsByTagName( "header" );
$header = $headers->item(0)->nodeValue;
$dates= $post->getElementsByTagName( "date" );
$date= $dates->item(0)->nodeValue;
$notes = $post->getElementsByTagName( "note" );
$note = $notes->item(0)->nodeValue;
$links = $post->getElementsByTagName( "link" );
$link = $links->item(0)->nodeValue;
echo '
<div class="blog">$header</div>
<div class="small">$date</div>
<div class="note">$note</div>
<a href="$link">$link</a> ';
}
?>
XML doc...
<blog>
<post>
<header></header>
<date></date>
<note></note>
<link></link>
</post>
</blog>
i create the exact xml and php files that you've written there, but when i test them on my site, i get an error like this
Parse error: parse error, unexpected T_OBJECT_OPERATOR in /homepages/1/d206546483/htdocs/jussieutik/test/tres.php on line 15
my php is like this, is this the problem??
Code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<?php
$doc = new DOMDocument();
$doc->load( 'employees.xml' );
$employees = $doc->getElementsByTagName( "employee" );
foreach( $employees as $employee )
{
$names = $employee->getElementsByTagName( "name" );
$name = $names->item(0)->nodeValue;
$ages= $employee->getElementsByTagName( "age" );
$age= $ages->item(0)->nodeValue;
$salaries = $employee->getElementsByTagName( "salary" );
$salary = $salaries->item(0)->nodeValue;
echo "<b>$name - $age - $salary\n</b><br>";
}
?>
<body>
</body>
</html>
I've been trying to get a php class to write into xml... I've looked around and all the code seems the same to the below and everyone i have tried is coming up with the same error msg..
Warning: domdocument() expects at least 1 parameter, 0
Has anyone else had this problem? Does anyone know of an answer?
Thanks
Also, note this other current thread--
http://www.dynamicdrive.com/forums/s...ad.php?t=24776
i try to write a xml file but i have error with :
"Warning: domdocument::domdocument() expects at least 1 parameter, 0 given in C:\xampp\htdocs\demo_ghifilexml\ghifilexml.php on line 2
Fatal error: Call to undefined method domdocument::load() in C:\xampp\htdocs\demo_ghifilexml\ghifilexml.php on line 3
"
i copy your code and paste in my php text so i understand what happen.
my php version:5.2.6
Uhhhh.... It looks to me that you are missing the user defined function 'load()'. Now, this brings to mind the need of importing the file (since that is what the load() function is supposed to do). This is only available in PHP5 and is part of the PHP XML DOM built-in processor. See: http://www.w3schools.com/php/php_xml_dom.asp and http://www.topxml.com/php_simplexml/...ction_load.asp
I've done this before using a php class to parse webpages and extract content, called dynfetcher, see: http://dynfetcher.googlecode.com/svn...cher.class.php
I think the code listed in the posts above is probably good if you are using PHP5.....
hi,
Please guide me how to modify xml using php.
hi,
Please guide me how to modify xml using php.
:confused: i don't know what happen ? help me !
Warning: domdocument::domdocument() expects at least 1 parameter, 0 given in C:\xampp\htdocs\phong_demo\duanmot\index.php on line 2PHP Code:<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("test.xml");
print $xmlDoc->saveXML();
?>
Fatal error: Call to undefined method domdocument::load() in C:\xampp\htdocs\phong_demo\duanmot\index.php on line 3
Very helpful thread and a good help to get some simple rss feeds pulled into other sites!
However I've a wee question to ask, is there any straight forward way to control how many of the nodes are parsed from the xml file?
Here's my current php to parse an xml feed;
The feed outputs lots of 'item' nodes but i only want the first 2 to parse from the php code.PHP Code:<?php
$doc = new DOMDocument();
$doc->load( 'xmlFeed.xml' );
$channel = $doc->getElementsByTagName( "item" );
foreach( $channel as $item )
{
$links = $item->getElementsByTagName( "link" );
$link = $links->item(0)->nodeValue;
$titles= $item->getElementsByTagName( "title" );
$title= $titles->item(0)->nodeValue;
$guids = $item->getElementsByTagName( "guid" );
$guid = $guids->item(0)->nodeValue;
echo "<p><a href=\"$link\"><img src=\"$guid\" alt=\"$title\"></a><br />$title</p>\n\n";
}
?>
Hope I make sense here and thanks again for everybodys input!
is it possible to append nodes to an existing xml file instead of creating a new one every time
Hey great code CODEEXPLOITER! very useful thank you!!
1) how can I add the XML encoding line to the php?
2) I typed the ">" character in the php file but in the XML file shows this instead "<", how can I fix that?
thanks again
i was searching a similar one...found this...
http://www.ksada.com/knowledge-base/php/read-xml-in-php
I created an account to reply to this response. I really need help making my website work right... I need to allow people to create and modify xml files. It needs to be in some kind of user friendly text editor. I dont want the user to have to enter any code tags or anything like that. Here is an example of my xml file.
everything that would need to be edited is within the cdata. I am just looking for ideas if anyone knows anything that could help I would greatly appreciate it!PHP Code:<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<content Name="LAWYER">
<item Border="10" Width="700" Height="500" ><![CDATA[<font color="#ffffff" size="70"><b>Michael T. Gilbert</b></font><br><font color="#ffffff" size="14"><br><br>MICHAEL T. GILBERT, born Manhasset, New York, July 9, 1954; admitted to bar, 1985, Colorado.<br><br><img src="content/mtg.png"><b>Contact Information:</b><br>Phone: (970) 325-4570<br>Fax: (970) 325-4957<br>E-Mail: <a href=" mgilbert@ouray.net"> mgilbert@ouray.net</a><br>P.O. Box 1359<br>Ouray, Colorado 81427<br><b>Area of Emphasis</b><br>Civil Litigation<br>Municipal Law<br><b>Education:</b><br>University of Colorado (B.A. English, 1977)<br>Cornell University (J.D. , magnum cum laude 1985)<br>Martindale-Hubbell Rating -AV<br><b>Experience:</b><br>2000 to present: Member Reed & Gilbert, Montrose, CO<br>1991 to 1999: Partner Williams, Youle & Koenigs, Denver, CO<br>1990 to 1991: Law Clerk to Judge E.W. Nottingham, US District Court, Denver, CO<br>1985 to 1990: Associate Sherman & Howard, Denver, CO<br><b>Practice Area</b><br>Trials and Appeals in federal and state courts; expertise in complex commercial litigation, such as real estate related matters, construction and contract disputes.<br><b>Community Interests</b><br>Michael enjoys the outdoors, including climbing, hiking, dog sled racing, and fishing.</font><br><br>]]></item>
</content>
broda put da version of xml in domdocument class like this
it ll solve ua problemPHP Code:<?php
$xmlDoc = new DOMDocument("1.0");
$xmlDoc->load("test.xml");
print $xmlDoc->saveXML();
?>
if any1 wana contact me add me on fsd.lions@gmail.com see ya
Hi everyone...I'm extremely new to both xml and php and I'm trying to get my bearings here.
I’ve looked at so much information but can’t find a simple script that will let me pull a simple object out of a xml if I already know what the id for that object is. Can someone get me started here? Say I want to pull the name for id 2 from the xml?
If you have a simple xml called friends.xml
<people>
<id>1</id>
<name>Joe</name>
</people>
<people>
<id>2</id>
<name>Mike</name>
</people>
With simplexml I can get to the file by doing
$myfriends = simplexml_load_file(friends.xml);
But I can't find any info on how to pull a single piece of info from an xml.
XML functions for PHP typically load the information into an array. From there you would just search the array at the right level. For example, it may be as simple as $myfriends[1], if that is how your XML is setup, but most likely instead it will mean looping through all of the array until you find one where ($thisfriend['id'] == 1).
A database will ALWAYS be better if you intend to search/cross-reference data. XML is possible, but for exactly the reasons you're seeing difficult and at some level of complexity may become impossible.
You would find a way to modify that age value. Find the right reference (such as $object->age or $object['age'], or however you are accessing it), then just change it using $var = $value; syntax.
THEN you need to write that entire XML structure back to the file. You can't just "change" it.
So if you are using functions that have something like xml_write_file($data), then you can do that, or you can construct it yourself by writing out the structure (easier using the first method). Then just store that XML into a file using fwrite() or maybe that's already built into your functions.
Still trying to figure this out. So I created a counter and am trying to read then add 1 to counter before replacing orginal number. I can read fine but the writing I've got all messed up.
Can someone tell me what I'm doing wrong here?
XML File named counter.xml
<?xml version="1.0" encoding="utf-8"?>
<counters>
<mycount>
<numcount>1</numcount>
</mycount>
</counters>
I get the count
<?php
//get count
$doc = new DOMDocument();
$doc->load('counter.xml');
$mycount = $doc->getElementsByTagName( "mycount" );
$numcount = $mycount->item(0)->nodeValue;
echo"<input type='text' name='count' id='count' value=$numcount>"
?>
Add 1 and write back...so I don't need an array with just one number but an array can have one number. I wanted to make it so it would work on things that have multiple elements.
<?php
//function called from web page
function newquote()
{
//change counter
$count = document.insertsidebar.count.value + 1;
//writing to counter xml
$mycount = array();
$mycount [] = array('mycount' => $count);
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement(counters);
$doc->appendChild($r);
foreach ($counters as $mycount)
{
$b = $doc->createElement( "counter" );
$count = $doc->createElement( "count" );
$b->appendChild( "counter" );
$r->appendChild( $b );
}
echo $doc->saveXML();
$doc->save("counter.xml");
}
?>
I see how to read and how to write.
How would you add a record after reading and before writing?
or is that what this part is?
$employees [] = array(
'name' => 'Albert',
'age' => '34',
'salary' => "$10000"
);
$employees [] = array(
'name' => 'Claud',
'age' => '20',
'salary' => "$2000"
);
how would you either make the form enter the post as first one (first child) into xml or get the php to load from the bottom up. in example: i want to load date and time items in order from most recent.
Below is the code in my slideshow.php which is behaving as XML properly But What want is My music and images should be dynamically fetched from database.How can i do that.
PHP Code:<?php
file_put_contents('slideshow.xml', '<?xml version="1.0" encoding="iso-8859-1"?>
<slideshow displayTime="5"
transitionSpeed=".7"
transitionType="Fade"
motionType="None"
motionEasing="easeInOut"
randomize="false"
slideshowWidth="400"
slideshowHeight="220"
slideshowX="center"
slideshowY="0"
bgColor="FFFFFF"
bgOpacity="100"
useHtml="true"
showHideCaption="false"
captionBg="000000"
captionBgOpacity="0"
captionTextSize="11"
captionTextColor="FFFFFF"
captionBold="false"
captionPadding="7"
showNav="false"
autoHideNav="false"
navHiddenOpacity="40"
navX="335"
navY="193"
btnColor="FFFFFF"
btnHoverColor="FFCC00"
btnShadowOpacity="85"
btnGradientOpacity="20"
btnScale="120"
btnSpace="7"
navBgColor="333333"
navBgAlpha="0"
navCornerRadius="0"
navBorderWidth="1"
navBorderColor="FFFFFF"
navBorderAlpha="0"
navPadding="8"
tooltipSize="8"
tooltipColor="000000"
tooltipBold="true"
tooltipFill="FFFFFF"
tooltipStrokeColor="000000"
tooltipFillAlpha="80"
tooltipStroke="0"
tooltipStrokeAlpha="0"
tooltipCornerRadius="8"
loaderWidth="200"
loaderHeight="1"
loaderColor="FF0000"
loaderOpacity="100"
attachCaptionToImage="true"
cropImages="false"
slideshowMargin="0"
showMusicButton="false"
music="[COLOR="Red"]upload/1.mp3[/COLOR]"
musicVolume="50"
musicMuted="false"
musicLoop="true"
watermark=""
watermarkX="625"
watermarkY="30"
watermarkOpacity="100"
watermarkLink=""
watermarkLinkTarget="_blank"
captionsY="bottom"
>
<image img="[COLOR="red"]upload/2.jpg[/COLOR]" customtitle="Camomile" />
<image img="[COLOR="Red"]upload/3.jpg[/COLOR]" customtitle="Bells" />
<image img="[COLOR="Red"]upload/4.jpg[/COLOR]" customtitle="Market" />
<image img="[COLOR="Red"]upload/5.jpg[/COLOR]" customtitle="Market" />
</slideshow>', LOCK_EX);
?>
hey guys,
Im having trouble with this . I need to convert my xml file to php . Can someone help me asap .
thanks :)
You should start a new discussion for a new question. Also, your question is vague. What does it mean to convert XML to PHP? Those aren't the same kind of files. Do you mean XHTML to PHP? Or do you want to store XML data in a PHP file or database?
I'm going to close this discussion now. Please repost in a new thread including more information. If any specific point in this discussion is relevant, you can link to it.