Jump to content

XML Parsing


Bakes

Recommended Posts

Ok, I am a complete and utter XML newb, and would like to have some RL examples of how to parse stuff (the tutorials just seem to confuse me).

 

Is there any chance that someone could post some examples of how to grab the following:

 

<Status Time="Sat May 02 14:13:01 2009">
<Clients Total="20">
	<Client Name="ClientName" DBID="48107" Level="2" GUID="a2d5b747c828d13370c564ec27b66e3d" IP="24.12.59.32" Joined="Wed Nov 12 20:16:38 2008" Updated="Sat May 02 14:11:46 2009">
		<Data Name="status" Value="OK"/>
		<Data Name="guid" Value="a2d5b747c828d13370c564ec27b66e3d"/>
	</Client>
</Clients>
<Type Name="TypeA" UsageLimit="None" Usage="2983">
	<Data Name="protocol" Value="6"/>
	<Data Name="Rate" Value="25000"/>
	<Data Name="_TimeStart" Value="1240971694.78"/>
</Type>
</Status>

 

Status -> Clients (want total) -> Client (want Name) -> Status (want values)

Status -> Type (want Name) -> (want protocol, rate).

 

Is there anyone who could post examples of how to get those values in simplexml?

 

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/156590-xml-parsing/
Share on other sites

<?php
/*
this DOM manual was great
http://phpbuilder.com/manual/en/function.dom-domelement-setattribute.php
props: php hacks Oreallly
by:JJVC- 787 457 4503 or [email protected] to contact me about questions.
*/
$titleFeed="s";//use _POST['']; //this data should come from a form send
$lastBuildDateInput="xx";
$pubDateInput="xcv";
$docsInput="kll";
$webMasterInput="jk";
/*********************************************************************************************
COMMENTS:

	The format of the date must be formatted correctly
**********************************************************************************************
i.e
Wed, 29 Apr 2009 08:00:00 PST
Follow the day and month convention.

day           =  "Mon"  / "Tue" /  "Wed"  /   "Thu" /  "Fri"  / "Sat" /  "Sun"
month         =  "Jan"  /  "Feb" /  "Mar"  /  "Apr"
               /  "May"  /  "Jun" /  "Jul"  /  "Aug"
               /  "Sep"  /  "Oct" /  "Nov"  /  "Dec"

*************************************************************************************************
*************************************************************************************************
END COMMENTS
*/

$books = array(
  array (
      id => 1,
      author => "Jack ",
      name => "Code Generation in Action"
    ),
  array (
      id => 2,
      author => "Jack Herrington",
      name => "Podcasting Hacks"
    ),
  array (
      id => 3,
      author => "Jack Herrington",
      name => "PHP Hacks"
    )
  );

$dom = new DomDocument("1.0");
$dom->formatOutput = true;

$rss = $dom->createElement( "rss" );
$rss->setAttribute("xmlns:itunes", "http://www.itunes.com/dtds/podcast-1.0.dtd");
$rss->setAttribute("version", "2.0");
$dom->appendChild( $rss );

$channel = $dom->createElement( "channel" );
$rss->appendChild( $channel );

$title   = $dom->createElement( "title" );
$channel -> appendChild($title);//title is a child of channel
$title ->appendChild( $dom->createTextNode($titleFeed) );


$lastBuildDate=$dom->createElement("lastBuildDate");
$channel -> appendChild($lastBuildDate);//lastBuildDate is a child of channel
$lastBuildDate ->appendChild( $dom->createTextNode($lastBuildDateInput) );

$pubDate=$dom->createElement("pubDate");
$channel -> appendChild($pubDate);//pubDate is a child of channel
$pubDate ->appendChild( $dom->createTextNode($pubDateInput) );


$docs = $dom->createElement("docs");
$channel -> appendChild($docs);//docs is a child of channel
$docs ->appendChild( $dom->createTextNode($docsInput) );

$webMaster = $dom->createElement("webMaster");
$channel -> appendChild($webMaster);//webMaster is a child of channel
$webMaster ->appendChild( $dom->createTextNode($webMasterInput) );

foreach( $books as $book )
{
  $item = $dom->createElement( "item" );



  $title = $dom->createElement( "title" );
  $title->appendChild( $dom->createTextNode( "hi" ) );
  $item->appendChild( $title );//author is a child of item

  $link = $dom->createElement( "link" );
  $link->appendChild( $dom->createTextNode( $book['name'] ) );
  $item->appendChild( $title );//link is a child of item


  $guid = $dom->createElement( "guid" );
  $guid->appendChild( $dom->createTextNode( $book['name'] ) );
  $item->appendChild( $guid );//guid is a child of item

  $description = $dom->createElement( "description" );
  $description->appendChild( $dom->createTextNode( $book['name'] ) );
  $item->appendChild($description);//description is a child of item

  $enclosure  = $dom->createElement( "description" );
  $enclosure ->appendChild( $dom );
  $item->appendChild($enclosure );//guid is a child of item



  $channel->appendChild( $item );
}

header( "Content-type: text/xml" );
echo $dom->saveXML();

?>

 

This is an work in progress but it will help you.

Link to comment
https://forums.phpfreaks.com/topic/156590-xml-parsing/#findComment-824513
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.