PHP read and generate XML file measurements
Last Update:2016-05-22
Source: Internet
Author: User
<span id="Label3"></p><p><p>PHP generated XML file method of four methods, Bo master Google the next http://www.phppan.com/2009/10/use-php-create-xml-file/this write is also good, It can be used to generate XML files using Php's ready-to-use domdocument method.</p></p><p><p>Note that Xmlwriter's way of generating requires PHP to turn on extended php-xml Support.</p></p><p><p>Here are the demo demos available for Bloggers:</p></p><pre><span style="color: #800080;"><span style="color: #800080;">$xml</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>DOMDocument (' 1.0 ', ' Utf-8 '<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _meetingattendee</span></span>=<span style="color: #800080;"><span style="color: #800080;">$xml</span></span>->createelement ("meetingattendee"<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml</span></span>->appendchild (<span style="color: #800080;"><span style="color: #800080;">$xml _meetingattendee</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _planningcode</span></span>=<span style="color: #800080;"><span style="color: #800080;">$xml</span></span>->createelement ("planningcode"<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _planningcode</span></span>->nodevalue = ' FPLN891 '<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _meetingattendee</span></span>->appendchild (<span style="color: #800080;"><span style="color: #800080;">$xml _planningcode</span></span><span style="color: #000000;"><span style="color: #000000;"> );</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _senddate</span></span>=<span style="color: #800080;"><span style="color: #800080;">$xml</span></span>->createelement ("senddate"<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _senddate</span></span>->nodevalue = ' 2016-04-23 00:00:00 '<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _meetingattendee</span></span>->appendchild (<span style="color: #800080;"><span style="color: #800080;">$xml _senddate</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _attendeelist</span></span>=<span style="color: #800080;"><span style="color: #800080;">$xml</span></span>->createelement ("attendeelist"<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _meetingattendee</span></span>->appendchild (<span style="color: #800080;"><span style="color: #800080;">$xml _attendeelist</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #800080;"><span style="color: #800080;">$i</span></span>= 0;<span style="color: #800080;"><span style="color: #800080;">$i</span></span>< 2;<span style="color: #800080;"><span style="color: #800080;">$i</span></span>++<span style="color: #000000;"><span style="color: #000000;">){</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _attendee</span></span>=<span style="color: #800080;"><span style="color: #800080;">$xml</span></span>->createelement ("Attendee"<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _attendeelist</span></span>->appendchild (<span style="color: #800080;"><span style="color: #800080;">$xml _attendee</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _attendeetype</span></span>=<span style="color: #800080;"><span style="color: #800080;">$xml</span></span>->createelement ("attendeetype"<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _attendeetype</span></span>->nodevalue = ' 1 '<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _attendee</span></span>->appendchild (<span style="color: #800080;"><span style="color: #800080;">$xml _attendeetype</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _attendeeid</span></span>=<span style="color: #800080;"><span style="color: #800080;">$xml</span></span>->createelement ("attendeeid"<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _attendeeid</span></span>->nodevalue = ' 16705 '<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _attendee</span></span>->appendchild (<span style="color: #800080;"><span style="color: #800080;">$xml _attendeeid</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _attendeehcpid</span></span>=<span style="color: #800080;"><span style="color: #800080;">$xml</span></span>->createelement ("attendeehcpid"<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _attendeehcpid</span></span>->nodevalue = ' DR00080055 '<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _attendee</span></span>->appendchild (<span style="color: #800080;"><span style="color: #800080;">$xml _attendeehcpid</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _attendeeguid</span></span>=<span style="color: #800080;"><span style="color: #800080;">$xml</span></span>->createelement ("attendeeguid"<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _attendeeguid</span></span>->nodevalue = ' dbca2553-7cd0-e211-9bb8-005056985885 '<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _attendee</span></span>->appendchild (<span style="color: #800080;"><span style="color: #800080;">$xml _attendeeguid</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _signedtime</span></span>=<span style="color: #800080;"><span style="color: #800080;">$xml</span></span>->createelement ("signedtime"<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _signedtime</span></span>->nodevalue = ' 2016-04-23 00:00:00 '<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #800080;"><span style="color: #800080;">$xml _attendee</span></span>->appendchild (<span style="color: #800080;"><span style="color: #800080;">$xml _signedtime</span></span><span style="color: #000000;"><span style="color: #000000;">);}</span></span><span style="color: #800080;"><span style="color: #800080;">$xml</span></span>->formatoutput=<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #800080;"><span style="color: #800080;">$xml</span></span>->save ("meeting.xml");</pre><p><p></p></p><p><p>The resulting XML file structure:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"><?</span></span><span style="color: #ff00ff;"><span style="color: #ff00ff;">XML version= "1.0" encoding= "utf-8"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">?></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Meetingattendee</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Planningcode</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>FPLN891<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Planningcode</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">senddate</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>2016-04-23 00:00:00<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">senddate</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">attendeelist</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Attendee</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Attendeetype</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>1<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Attendeetype</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Attendeeid</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>16705<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Attendeeid</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Attendeehcpid</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>DR00080055<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Attendeehcpid</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Attendeeguid</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>dbca2553-7cd0-e211-9bb8-005056985885<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Attendeeguid</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Signedtime</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>2016-04-23 00:00:00<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Signedtime</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Attendee</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Attendee</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Attendeetype</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>1<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Attendeetype</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Attendeeid</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>16705<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Attendeeid</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Attendeehcpid</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>DR00080055<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Attendeehcpid</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Attendeeguid</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>dbca2553-7cd0-e211-9bb8-005056985885<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Attendeeguid</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Signedtime</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>2016-04-23 00:00:00<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Signedtime</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Attendee</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">attendeelist</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Meetingattendee</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><p><p></p></p><p><p>There are also several ways to read the two types:</p></p><p><p>one, DOMDocument Way Read:</p></p><pre><span style="color: #800080;"><span style="color: #800080;">$doc</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">DOMDocument ();</span></span><span style="color: #800080;"><span style="color: #800080;">$doc</span></span>->load (' Test1.xml ');<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">reading an XML file</span></span><span style="color: #800080;"><span style="color: #800080;">$MeetingAttendee</span></span>=<span style="color: #800080;"><span style="color: #800080;">$doc</span></span>->getelementsbytagname ("meetingattendee");<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">an array of objects that have been tagged</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">foreach</span></span>(<span style="color: #800080;"><span style="color: #800080;">$MeetingAttendee</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> as</span> <span style="color: #800080;"><span style="color: #800080;">$row</span></span><span style="color: #000000;"><span style="color: #000000;"> ) { </span></span><span style="color: #800080;"><span style="color: #800080;">$PlanningCodes</span></span>=<span style="color: #800080;"><span style="color: #800080;">$row</span></span>->getelementsbytagname ("planningcode");<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Object array of the obtained label</span></span><span style="color: #800080;"><span style="color: #800080;">$PlanningCode</span></span>=<span style="color: #800080;"><span style="color: #800080;">$PlanningCodes</span></span>->item (0)->nodevalue;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get the value in the tag</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Echo</span></span> <span style="color: #800080;"><span style="color: #800080;">$PlanningCode</span></span><span style="color: #000000;"><span style="color: #000000;">;} </span></span></pre><p><p>second, simplexml Mode Read</p></p><pre><pre><span style="color: #800080;">$xml _object</span> <span style="color: #008080;">simplexml_load_file</span> (<span style="color: #800080;">$localfile</span><span style="color: #000000;">)</span> <span style="color: #0000ff;">Echo</span> <span style="color: #800080;">$xml _object</span>->meetingattendee->planningcode;</pre></pre><p><p></p></p><p><p>PHP read and generate XML file measurements</p></p></span>