20150202--xml under 02

Source: Internet
Author: User
Tags xpath functions

Third, PHP simplexml implementation of XML additions and deletions 1, load XML file into memory, and form SimpleXML object

SimpleXMLElement simplexml_load_file (String $filename): Load XML file into memory and generate SimpleXML object

Simplexmlelement::__construct (String data)

Example code:

Operating effect:

2. How to enter the value of the specified text node

Operating effect:

3, how to traverse the output all data?

Method One: Iterate through the array directly via foreach

Example code:

Operating effect:

Method Two:

Using the Children () method to implement the traversal output of XML data

The method can get directly to the child node of the current object

Example code:

4. How to Output properties?

In the SimpleXML object, the reading of the property is special and cannot be read directly by the above method, it must be done by external means:

Directly read by array:

Example code:

Get to the Property object through the attributes () method and enter the value of the property

Example code:

5, expand the case: Query the weather forecast and give the corresponding suggestions

Ideas:

1) Get China weather Net API interface directly via Ajax

2) can be provided through the Baidu Development Platform API interface

A) Open the Baidu open platform

Website: http://developer.baidu.com/

b) Find the LBS cloud and find the car networking API open as shown in

By observing the above, it can be found that the system only provides a URL address, because we visit a URL, is actually to send HTTP requests to the URL, the request is divided into two kinds: Get request and POST request

1) Read the interface instructions to determine if the request is a GET or post

2) directly copy URL URL to the browser address bar, if you can get the data normally means that it is a GET request

Enter the above API address to return data to prove that the current request is a GET request

Then we can use file_get_contents () to simulate a GET request

By analyzing its URL, we can find that at least two parameters are required

Location: City Name

AK: Developer Key

Example code:

Operating effect:

6. SimpleXML Add node

The knowledge points to use:

Public simplexmlelement Simplexmlelement::addchild (string $name [, String $value])

Parameter description:

$name: Name of the node created

$value: Optional parameter, the value of the node

Note: In SimpleXML, the Add node can only be added from top to bottom, there is no way to create from below, remember!!!!!

Save XML file: Asxml

Parameter description:

$filename: To save the file name

Add attribute: AddAttribute () method

public void Simplexmlelement::addattribute (string $name [, String $value [, String $namespace]]

Parameter description:

$name: Property Name

$value: Property values

Example code:

Operating effect:

7, simplexml Modify and delete operation

Modify the operation, sample code:

Delete operation, sample code:

Iv. Xpath

Mysql:sql statement for finding operations on data

Xml:xpath to implement a lookup operation on XML data

Mainly used for accurate query, more detailed query

Xpath: Path Query Language: 1, query data with absolute path

In the XPath query command, you can pass/represent the absolute path to query

Note: If you use the absolute path in XPath, then query, from the root element, one layer down the query, not leapfrog.

Example:

/AAA represents starting from the root element, since there is only one AAA element behind it, so the current query represents the query root element AAA

Question: How do I determine which node to query?

/aaa/bbb/ccc

A: If we want to determine which node to query, we just need to determine the name of the node followed by the last slash (single oblique or double oblique)

You can use XPath commands in SimpleXML to combine queries by using XPath functions in the

Parameter description

$path: XPath query command

Operation Result:

Case: Querying all title node information through XPath

Operating effect:

2. Querying data using relative paths

In the XPath language, node information can be retrieved through a pair of double slashes///, without hierarchy

Operation Result:

Case: Querying all book nodes in the current project through XPath

Operating effect:

3. Search all data by asterisk *

In the XPath language, you can query all the elements in a location by *

Case: Get all child nodes under the book node

Description

In the XPath language, you can get the number of child nodes by * asterisk, but there is no way to get the value inside the asterisk directly.

4. Use [] square brackets for conditional query

can be passed by [index]: The default index is starting from 1

You can also pass [last ()]: Get the final element

Example code:

Operating effect:

5. Attribute query by the @ symbol

Case: Get all the book elements with attributes in the Library.xml file through @*

Operation Result:

6. Extend the [] conditional query in XPath

Case: Through [] Query query author for the book Information of Wang Choi

Operating effect:

V. Case: Online Word Blaster 1, interface design

2. Design Database

Data.xml

3. Backstage system Design

Experience: How to determine the first design of the module background or foreground, first of all, please determine where the data in the database from, if it is from the background, then first implement the background, such as the front desk, then please design the front desk.

4. The foreground function realizes:

Operating effect:

Job Two: Online word blaster

20150202--xml under 02

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.