xmlKnowledge summary
xml structure
Document statement:
Attributes
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-stylesheetCommand
Example:Comment:
Comments can be displayed in the browser
XMLThere can be no comments before the statement
,Comments cannot Nested
CDATAsection
Syntax:Content]]>xml DTD
Document Constraints
The constraint document definesxmlAllowed element names, attributes, and element appearance order, etc.
DTD Constraint document classification
InternalDTD
Root element [DTD Constraints
]>PublicDTD
Root Element
PUBLIC "DTDIdentity 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 directlyto define the entity
Reference the entityBasic 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 methoddomdocument_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
domTree
$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
namenode 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 nodestring DOMElement::getAttribute(string $name);
PHP DOM
Add, delete, modify and check operationsPHP 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 DOMAttributes 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
xmlData
obtained through the
foreach()method 0]->nodeName
Get it directlyxml
Add element/***@param string $str
The definedxml
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
xmlfile 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.