Home >Backend Development >PHP Tutorial >PHP creation and parsing XML_PHP tutorial
As XML continues to heat up, PHP provides a large number of standard libraries (class libraries) specifically designed to handle XML to manipulate XML. For example, DOM XML extension rewriting, SimpleXML extension, SOAP extension, and PHP5.1's XMLReader and XMLWrite extensions.
one. Manipulate XML with SimpleXML
To process XML files, there are two traditional processing ideas: SAX and DOM. Based on the event triggering mechanism, SAX scans the XML file once and completes the processing; DOM constructs the entire XML file into a DOM tree, and completes the processing by traversing the DOM tree. Both methods have their own advantages and disadvantages. The processing idea of SAX is relatively abstract, and the processing process of DOM is relatively cumbersome. Neither of them is very suitable for novices to get started. PHP5 introduces a new set of XML processing functions, SimpleXML. As the name suggests, SimpleXML itself is small and compact, providing only a few methods and functions, but it is very powerful and easy to operate when processing XML files.
[php] view plaincopy
1. Create XML file
$ _ xml = <<
xml;
$ _sxe = new SimpleXMLElement( $ _xml ); // Create object to parse xml string
$ _sxe ->asXML( 'test.xml' ); // Generate XML file
2. Load XML file
$ _sxe = simplexml_load_file( "test.xml" ); // Load XML file
var_dump( $ _sxe ); // Output related information
print_r( $ _sxe ); // Output main information
Reflection::export( new ReflectionClass( $sxe )); // Use reflection to view details
3. Parse XML files
$ _sxe = simplexml_load_file( "test.xml" ); // Load XML file
var_dump( $ _sxe ); // Output related information
print_r( $ _sxe ); // Output main information
Reflection::export( new ReflectionClass( $ _sxe )); // Use emission to view details
echo $_ sxe ->asXML(); // Print the entire XML
4. Read XML data
$_ sxe = simplexml_load_file( "test.xml" );
// Read the value of the first-level node, such as version tag
echo $_ sxe ->version;
// If there are multiple, you can set its numerical subscript
echo $_ sxe ->version[ 2 ];
// If you want to print them all, you can use traversal
foreach ( $_ sxe ->version as $_version ) {
echo '[' . $_version . ']' ;
}
//Access the name of the secondary node
echo $_ sxe ->user[ 1 ]->name;
// Get the name value of all secondary nodes
foreach ( $_ sxe ->user as $_user ) {
echo '[' . $_user ->name. ']' ;
}
// Get the attributes of the label of the secondary node
echo $_ sxe ->user[ 1 ]-> author ->attributes();
5. Use XPath to get nodes
$_ sxe = simplexml_load_file( "test.xml" );
// Use XPath to obtain node information www.2cto.com
$_version = $_ sxe ->xpath( '/root/version' );
echo $_version [ 1 ];
// Traverse version
foreach ( $_version as $_v ) {
echo '[' . $_v . ']' ;
}
//Access secondary nodes
$_user = $_ sxe ->xpath( '/root/user' );
echo $_use r [ 2 ]->name;
// Traverse secondary nodes
foreach ( $_user as $_u ) {
echo '[' . $_u ->name. ']' ;
}
//Access properties
echo $_user [ 1 ]->author->attributes();
two. Use DOMDocument to manipulate XML
In many cases, manual generation of tags requires generating documents from top to bottom. It is necessary to ensure that the tags are complete, including start and end tags. Although it can be improved with the help of some PHP functions or classes, PHP also provides a more helpful set of built-in objects and functions. The Document Object Model (DOM) provides a tree structure that makes it easy to create and process tags.
[php] view plaincopy
1. DOMDocument parses XML
// Create a DOMDocument()
$_doc = new DOMDocument();
//Load xml
$_doc ->load( 'test.xml' );
// Get version tag
$_version = $_doc ->getElementsByTagName( 'version' );
echo $_version ->item( 2 )->nodeValue;
// Traverse version tags
foreach ( $_version as $v ) {
echo $v ->nodeValue;
}
2. DOMDocument generates XML
// declaration xml
$_doc = new DOMDocument( '1.0' , 'utf-8' );
// Formatting
$_doc ->formatOutput = true ;
//Create a main tag
$_root = $_doc ->createElement( 'root' );
//Create a first-level tag version
$_version = $_doc ->createElement( 'version' );
// Assign value to version tag
$_versionTextNode = $_doc ->createTextNode( '1.0' );
//Put the value into the version tag
$_version ->appendChild( $_versionTextNode );
//Put the first-level label version into root
$_root ->appendChild( $_version );
//Write the main tag into xml
$_doc ->appendChild( $_root );
// Generate xml
$_doc ->save( 'aaa.xml' );