Home > Backend Development > PHP Tutorial > xml knowledge points sorting out - compiled from Guangzhou Chuanzhi Wang

xml knowledge points sorting out - compiled from Guangzhou Chuanzhi Wang

WBOY
Release: 2016-08-08 09:26:40
Original
1362 people have browsed it

xmlKnowledge summary

xml structure

Document statement:

An element can have multiple attributes

The attribute value must be quoted

(

single quote or double quote

)The naming convention of the attribute name is the same as the naming convention of the element Attributes in elements are not allowed to be repeated

The information represented by the tag attribute can also be changed to be described in the form of sub-elements

Processing instructions

must start with

"

and end with

"?>"as the endxmlCommand

xml-stylesheet

Command

Example:

Comment:

Comments can be displayed in the browser

XMLThere can be no comments before the statement

,

Comments cannot Nested

CDATA

section

Syntax:

Content

]]>

xml DTD

Document Constraints

The constraint document defines

xmlAllowed element names, attributes, and element appearance order, etc.

DTD Constraint document classification

Internal

DTD

Root element [

DTD Constraints

]>

PublicDTD

Root Element

PUBLIC "DTD

Identity Name" "PublicDTDURI">

DTD

Constraint writing Define element ( tag or tag )Element name Element type >Element type

(#PCDATA) :

This type contains any character data and cannot Contains child elements

EMPTY:

This type cannot contain child elements or text, and can have attributes

ANY : This element contains all content (applicable to root elements)

(child elements) : Define child elements can only appear once

(child element 1

, child element

2, ...) : elements appear in the specified name and order

(child element

+)

: The current element appears 1+ times

(

child element*) : The current element appears 0+ times

(

child element? ) : Current element query 0 or 1

times (child element 1 | child element 2)

: Mutually exclusive elements, can only be selected 1Define attributesBasic syntax: Element name Attribute name Attribute type Default value >Attribute type

CDATA : Character or number (text type)

( en1|en2|en....) : Enumeration type ID: Unique attribute IDREF: Reference to the defined

ID

attribute

#REQUIRED : Required (default Value)

#IMPLIED:

Optional

#FIXED:

Fixed value

Value: Use the default value directly

to define the entity

Reference the entity

Basic syntax: Entity name

"

value of entity

">

Quotation syntax:

[

used in

xml

]

&Entity Name ;

parameter entity [parameter entity must be defined in the external DTD, in the external dtd

Used in ] Basic grammar:

Entity name "Entity value">

Quotation syntax:

%Entity name;

PHPparsing XML Data

PHP DOM Model analysis DOMNode : Node class

DOMElement : Node class

DOMDocumentClass:

Basic syntax:DOMDocument::__construct( string $version , string $encoding ){}

load

Usage of method

domdocument_obj->load ( 'xml_filename.xml' ) ;

item method usage:

domdocument_obj -> item(int index);

Case: PHP DOM

query operation

[

1

] open up memory space to form

noodles

"

$dom = new DOMDocument('1.0','utf-8');

[2] loads the xml file into memory to form

dom

Tree

$dom->load( 'test.xml' );3】Get the sibling nodes of the same level to form a string $names = $dom->getElementsByTagName( 'Element name

');

[4] Get the

name

node information whose node name is Wang Dachui, the so-called point $name = $names -> nodeValue;

The above code is simplified: $dom->getElementsByTageName('name')->item(0)->nodeValue;PHP DOM Attribute operation

#Judge a node Whether it has the specified attributes

bool DOMElement::hasAttribute(string $name);

#Determine whether a node has the specified attributes

bool DOMNode::hasAttributes(void);#

Get a certain The value of the specified attribute of a node

string DOMElement::getAttribute(string $name);

PHP DOM

Add, delete, modify and check operations

PHP DOM

Add operation:

Method to create a new node:

DOMDocument :: createElement(string name [ ] );

Append new node:

DOMDocument :: appendChild(DOMNode $newnode);

Save operation:

DOMDocument :: saveXML( filename);

PHP DOM

Delete operation:

Remove node operation:

DOMNode :: removeChild( DOMNode $nodeName ) ;

PHP DOM

Change operation:

Update node operation:

/**

*

@param string $newNode Updated node

* @param string $oldNode Old node

*/DOMNode :: replaceChild( DOMNode $newNode , DOMNode $oldNode ) ;

PHP DOM

Attributes Operation: Add attributes to the node:

/**

*

@param string The name of the attribute to be added

* @param string

The value of the attribute

*/setAttribute(string $name , string $value );

Update node attributes:

/**

*

@param string The name of the attribute to be updated

* @param string

The value of the attribute

*/setAttribute ( string $name , string $value ) ;

Remove the attributes of the specified node:

/**

*

@param string The name of the attribute to be deleted

*/

removerAttribute ( string $name );

Basic grammar:

/*

* @param string $filename The xml to be loaded

File name

*/

simpleXMLElement simplexml_load_file(string $filename );

+ )method to get

xml

Data

obtained through the

foreach()

method 0]->nodeName

Get it directly

xml

Add element/***@param string $str

The defined

xml

character content

*/

SimplexmlElement::addChild(string $name [,string $vaule]);

# After adding, save it /**

* @param string $name The name of the added element

* @param string $value Set the value

*/

simpleXMLElement :: asXML(string $filenmae );xml Attribute query

/**

* @param string $filename The saved

xml

file name

*/SimpleXMLElement :: attributes()->attr_name;

xml Addition of attributes

/**

* @param string attr_name

The name of the added attribute

*/

smpleXMLElement::addtribute( string $ name , [string $value [,string $namespace ]]

Bracket expression@Attribute

The above introduces the sorting out of xml knowledge points - compiled from Guangzhou Chuanzhi Wang, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template