PHP Operations Xml,xml Common

Source: Internet
Author: User
Tags file transfer protocol
First, the solution Common protocol:
FTP (File Transfer Protocol): Remote file Transfer Protocol that allows users to copy files from a remote host to their own computer.
SMTP (Easy Mail Transfer Protocol): Simple postal Transport protocol for the transfer of e-mail.
NFS: Network file server, which enables multiple computers to transparently access each other's directories.
HTTP Hypertext Transfer Protocol:
HTTP is an application-level Object OrientedThe protocol, due to its simple and fast way, is suitable for distributed hypermedia information System. It was proposed in 1990, after several years of use and development, has been continuously improved and expanded. Currently used in the WWW is the sixth edition of Http/1.0, http/1.1 standardization work is in progress
The main features of the HTTP protocol can be summarized as follows:
1. Support client/server mode.
2. Simple and fast: When a customer requests a service from the server, it simply transmits the request method and path. The request method commonly has, POST. Each method specifies a different type of contact between the customer and the server.
Because the HTTP protocol is simple, the HTTP server's program size is small, so the communication speed is fast.
3. Flexible: HTTP allows the transfer of any type of data object. The type being transmitted is marked by Content-type.
4. No connection: The meaning of no connection is to limit the processing of only one request per connection. When the server finishes processing the customer's request and receives the customer's answer, the connection is disconnected. In this way, the transmission time can be saved.
5. Stateless: The HTTP protocol is a stateless protocol. Stateless means that the protocol has no memory capacity for transactional processing. A lack of state means that if the previous information is required for subsequent processing, it must be re-routed, which may cause the amount of data to be transferred per connection to increase. On the other hand, it responds faster when the server does not need the previous information.
Second, understand the XML:
When XML (extended Markup Language) was introduced into software industry in February 1998, it brought a storm to the industry. For the first time in history, the world has a versatile and adaptable format for structured documents and data that can be used not just for the WEB, but for any place.
XML refers to the extended markup language.
HTML refers to Hypertext Markup language.
PHP refers to hypertext preprocessing languages.
XML is a bridge of communication between different languages of different platforms.
XML is essentially a text file. The structure used to describe the data
Basic syntax FOR XML:
XML has strict syntax rules
XML must first declare

The XML must create the root element
All elements in the XML must appear in pairs
The XML is case sensitive
XML element nesting must be correct
The attributes of an XML element must use the containing
XML documents use self-describing and simple syntax, and the most basic composition of an XML document includes: Declaration, processing instructions (optional), and elements. The following is a simple



My Web page
Lyle_zhang
This is the content @! The Magical 2013! I wish the students to find their ideal job!

Explain:
Declaration of the document

The XML tag indicates that it is an XML document, the following two attribute values indicate its version number and encoding standard, and standalone yes indicates that the file does not reference another external XML file.
Third, XML-related webservice.
XML Web Service is the basic building block for distributed computing on the Internet. Open standards and the focus on communication and collaboration between users and applications have created an environment in which XML Web Service becomes the platform for application integration.
XML Web Service can describe its interfaces in great detail, which enables users to create client applications to communicate with them.
XML Web Service provides useful functionality to Web users through a standard web protocol. In most cases, the SOAP protocol is used.
SOAP is the communication protocol for XML Web Service.
Four, the difference between XML HTML XHTML DHTML:
The difference between XML and HTML design is:
XML is used to store data, and it weighs on the data itself.
HTML is used to define the data, and the display mode of the data is heavy.
XHTML (the Extensible Hypertext Markup Language (Extensible Identity language)) is actually an upgraded version of HTML. The purpose of this release is to make it easier for people to accept XML, because XML is the direction of future web development, essentially, XHTML is a transition technology that combines the power of some XML with the simple nature of most HTML.
DHTML: To be exact, DHTML is just a concept of making Web pages, and virtually no organization or institution has ever introduced so-called DHTML standards or technical specifications. DHTML is not a technology, standard, or specification, and DHTML is a design concept that uses existing Web technologies and language standards to create pages that can still transform page elements in real time after they are downloaded. DHTML is based on the original technology and can be divided into three areas:
The first is HTML, which is the various page element objects in the page, they are the content that is manipulated dynamically;
Second, the Css,css attribute is also the content of dynamic manipulation, so as to obtain the dynamic format effect;
The third is client-side scripting (such as JavaScript), which actually manipulates HTML and CSS on a Web page.
Using DHTML technology enables Web page designers to create pages that interact with the user and contain dynamic content. In fact, DHTML enables web designers to manipulate all the elements on a Web page dynamically-even after those pages are loaded. With DHTML, web designers can dynamically hide or display content, modify style definitions, activate elements, and position elements. DHTML also enables Web page designers to display external information on a Web page by bundling elements into external data sources, such as files and databases. All of these features can be done by the browser without requesting Web Server, and you don't need to reload the page. This is because everything is included in the HTML file and is downloaded to the browser side as a request to the Web page.
V. DOM Operation XML


Aa
Bb
Cc

DOM node reads data
Header ("content-type:text/html; Charset=utf-8 ");
DOM node reads XML file
$dom = new DomDocument (); Creating DOM objects
$dom->load (' 1.xml '); Reading an XML file
$root = $dom->documentelement; Gets the root of the XML data
$nodes = $root->childnodes; Gets all child node objects under the XML root object
Echo $nodes->item (1)->nodevalue; Output name
Echo $nodes->item (3)->nodevalue; Output password
Echo $nodes->item (5)->nodevalue; Output gender
Echo $nodes->item (7)->nodevalue; Output age
Get Properties
echo $dom->getelementsbytagname (' name ')->item (0)->attributes->getnameditem (' abc ')->nodevalue;
Echo $nodes->item (1)->attributes->getnameditem (' id ')->nodevalue;
READXML ($root); Executes the loop read node function, passing the root node object
function ReadXml ($nodes) {
if ($nodes->haschildnodes ()) {//To determine if there are child nodes
$childNodes = $nodes->childnodes; Gets a list of all the byte points
foreach ($childNodes as $key + $node) {//Looping child nodes list
if ($node->nodetype = = Xml_element_node) {//Determine if it is a node object
READXML ($node);//recursive Execution of readXml () function
}elseif ($node->nodetype== xml_text_node) {//Determine if it is a file node
Echo $node->nodevalue; If there's business logic here, write your code here.
}
}
}
}
?>
DOM generates an XML document
$doc =new DOMDocument (' 1.0 ');
Create and Append node objects sequentially
$root = $doc->appendchild ($doc->createelement (' School ')); Create and append a root directory
Create child nodes
$class 1= $root->appendchild ($doc->createelement (' Class1 '));
Create a child node below the class
$stu = $class 1->appendchild ($doc->createelement (' Stu '));
Create attributes and text for Stu
$stu->appendchild ($doc->createtextnode (' 1 classes '));
$stu->setattribute (' number ', ' 10 ');
$stu->setattribute (' Zhuanye ', ' Java ');
/**
* The above code generates XML format


Class 1


*/
Create child nodes
$class 2= $root->appendchild ($doc->createelement (' class2 '));
Create a child node below the class
$stu 2= $class 2->appendchild ($doc->createelement (' Stu '));
Create attributes and text for Stu
$stu 2->appendchild ($doc->createtextnode (' 1 classes '));
$stu 2->setattribute (' number ', ' 20 ');
$stu 2->setattribute (' zhuanye ', ' php ');
Generated
$doc->formatoutput=true;
$xmlDoc = $doc->savexml (); Generate XML returns an XML document
$doc->save (' school.xml ');
/**
The result of the above code is eventually generated:



Class 1


Class 1


*/
Vi.. SimpleXML interface
Header ("content-type:text/html; Charset=utf-8 ");
$xml =simplexml_load_file (' school.xml ');//returns an array object
Looping through data and properties
foreach ($xml as $key = = $node) {
if ($key = = ' Class1 ') {
echo $node->stu;//Direct access to the object's text node//output Result: Class 1
}elseif ($key = = ' Class2 ') {
echo $node->stu[' number ']; Property Access output: 20
}
}
Direct access to Properties
echo $xml->class1->stu[' Zhuanye ']; Output Result: Java
/**



Class 1


Class 2


*/
Get XML remotely and save
Header ("content-type:text/html; Charset=utf-8 ");
Remote Access XML file
$xml =simplexml_load_file (' http://127.0.0.1/xml/2.xml ');
$xml->asxml (' 5.xml '); Save on Local
Manipulate your XML documents according to your business logic
?>
Seven
Database data Generation XML document
/**



1
Aa
Bb
Cc


**/
$c >mysql_select_db (' member ');
Mysql_set_charset ("UTF8");
Data submission
if (Isset ($_post[' zhuce ')) {
$username =trim ($_post[' username ');
$userpwd =trim ($_post[' userpwd ');
$useremail =trim ($_post[' useremail ');
Write to Database
$sql = "INSERT INTO reg (' id ', ' name ', ' pwd ', ' email ') VALUES (null, ' $username ', ' $userpwd ', ' $useremail ')";
$res = mysql_query ($sql);
if ($res) {
Querying the database
$sql = "SELECT * from Reg";
$rest =mysql_query ($sql);
$users =array ();
while ($rows =mysql_fetch_assoc ($rest)) {
$users []= $rows;
}
}
Determine if there is data
if (! empty ($users)) {
$xmlString =createxml (' users ', $users);
$XMLOBJ =new simplexmlelement ($xmlString); Generating an XML object
$XMLOBJ->asxml ("Reg.xml"); Save
}
}
Creating XML
function Createxml ($boot, $data) {
$xml = "< $boot >\n";
foreach ($data as $key = = $value) {
if (is_string ($key)) {
$xml. = "< $key >";
foreach ($value as $k = = $val) {
$xml. = "\n< $k >". $val. " ";
}
$xml. = "\ n \ n ";
}else{
$xml. = "< $boot. $key >";
foreach ($value as $k = = $val) {
$xml. = "\n< $k >". $val. " ";
}
$xml. = "\ n \ n ";
}
}
$xml. = " ";
return $xml;
}

?>





<title>Write data to the database after registration and save the data generation XML document under the API directory</title>





To get objects remotely:

$xml =simplexml_load_file (' http://127.0.0.1/xml/reg.xml ');
$MEMBERXML = $xml->asxml (' member.xml ');
?>





<title>Write data to the database after registration and save the data generation XML document under the API directory</title>














foreach ($xml as $key = = $node) {?> }?>
ID name pwd Email
ID;? > name;? > pwd;? > email;? >



Yahoo query China city ID address
http://weather.yahoo.com/china/
$xml =simplexml_load_file ("Http://xml.weather.yahoo.com/forecastrss?w=2158433&u=c");
Echo '
';
Var_dump ($xml);
$arrobj = $xml->channel->image;
Echo $arrobj->url;
Print_r ($xml->channel);
Echo '
';

echo ' URL. ' " > ';

echo ' link. ' " > '. $arrobj->title. ';
Sian Weather Weather ForecastXml
Header ("Content-type:text/html;charset=utf-8");
$c//php.weather.sina.com.cn/xml.php?password=djoyniet8234jlsk&day=0&city= Chengdu ");
File_put_contents (' Sina_weather.xml ', $content);

The above describes the PHP operation Xml,xml commonly used, including the Web server, weather forecast, object-oriented content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.