Home Backend Development PHP Tutorial 使用PHPDOM-XML创建和解析XML文件sdyqingdao(翻译)_PHP

使用PHPDOM-XML创建和解析XML文件sdyqingdao(翻译)_PHP

Jun 01, 2016 pm 12:35 PM
c print use create document translate parse


  /**

  * Topic:       Create and parse XML files using PHP DOM-XML

  * Source:      http://www.php.net/domxml

  * Reference:   http://www.zugeschaut-und-mitgebaut.de/php/extension.domxml.html

  * Author:      urs@circle.ch, 16-1-2001

  *

  */
 

// 使用PHP DOM-XML创建和解析XML文件



  //创建XML文档对象;以后的处理过程将在此基础上进行

  $doc = new_xmldoc("1.0" ); 



  //创建根节点,并设置一个属性

  $root = $doc->add_root("faq" ); 

  $root->setattr("page", "32" ); 



  //子节点

  $one = $root->new_child("question", ""); 

  //为子节点设置属性

  $one->setattr("number", "1"); 

  //question也创建子节点,并且给它赋值

  $one->new_child("text", "1. Where to get libxml-2.0.0?"); 

  $one->new_child("answer", "You can download the latest

  release of libxml   either as a source archive or

  RPM package from http://www.xmlsoft.org.

  The current version is libxml2-2.2.1." ); 



  $two = $root->new_child("question", "" ); 

  $two->setattr("number", "2"); 

  $two->new_child("text", "2. How to configure PHP4?" ); 

  // 创建一个不直接赋值的节点

  $twoone = $two->new_child("answer", ""); 

  // 然后给它单独赋值

  $twoone->set_content("DIR is the libxml install directory

  (if you just use --with-dom it defaults

  to /usr), I needed to use --with-dom=/usr/local" ); 



  $three = $root->new_child("question", "" ); 

  $three->setattr("number", "7" ); 

  $three->new_child("text", "7. How to use DOM XML function ?" ); 

  $three->new_child("answer", "Read this document source for

  a simple example." ); 



  //输出到Browser

  print("
".htmlspecialchars($doc->dumpmem() )."
Copy after login
" ); 



  // write to file

  //写回到文件

  $fp = fopen("test_dom.xml", "w+" ); 

  fwrite($fp, $doc->dumpmem(), strlen($doc->dumpmem() )); 

  fclose($fp); 



  // ------------------------------------------------------

  //现在使用xpath从XML文档中得到内容

 

  $doc = xmldoc(join("", file("test_dom.xml")) ); 

  $ctx = xpath_new_context($doc ); 



  //所有对象

  $foo = xpath_eval($ctx, "//child::*"); 

  print_r($foo); 

  print("

"); 

  //text node 对象

  $foo = xpath_eval($ctx, "//text"); 

  print_r($foo); 

  print("

"); 

  // 第一个text node对象

  $foo = xpath_eval($ctx, "//text[1]"); 

  print_r($foo); 

  print("

"); 

  // 第二个text node对象

  $foo = xpath_eval($ctx, "//text[2]"); 

  print_r($foo); 

  print("

"); 

  // 第三个answer对象

  $foo = xpath_eval($ctx, "//answer[3]"); 

  print_r($foo); 

  print("

"); 



  //第三个text node的类型,名称和内容

  $foo = xpath_eval($ctx, "//text[3]"); 

  $tmp = $foo->nodeset; 

  print_r($tmp); 

  print("
"); 

  print($tmp[0]->type) . "; "; 

  print($tmp[0]->name) . "; "; 

  print($tmp[0]->content); 



?> 



需要说明
PHP DOM 只能在PHP  PHP4.0.x + linux上运行



PHPDOM类库请到http
://www.zend.com/download
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. Mar 21, 2024 pm 09:17 PM

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box "Error 0x80004005: Unspecified Error" will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

Don't worry about watching movies without subtitles! Xiaomi announces the launch of Xiaoai Translation real-time subtitles for Japanese and Korean translation Don't worry about watching movies without subtitles! Xiaomi announces the launch of Xiaoai Translation real-time subtitles for Japanese and Korean translation Jul 22, 2024 pm 02:11 PM

According to news on July 22, today, the official Weibo of Xiaomi ThePaper OS announced that Xiaoai Translation has been upgraded. Real-time subtitles have been added to Japanese and Korean translations, and subtitle-free videos and live conferences can be transcribed and translated in real time. Face-to-face simultaneous interpretation supports translation into 12 languages, including Chinese, English, Japanese, Korean, Russian, Portuguese, Spanish, Italian, French, German, Indonesian, and Hindi. The above functions currently only support the following three new phones: Xiaomi MIX Fold 4 Xiaomi MIX Flip Redmi K70 Extreme Edition It is reported that in 2021, Xiao Ai’s AI subtitles will be added to Japanese and Korean translations. AI subtitles use Xiaomi’s self-developed simultaneous interpretation technology to provide a faster, more stable and accurate subtitle reading experience. 1. According to the official statement, Xiaoai Translator can not only be used in audio and video venues

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

How to create a folder on Realme Phone? How to create a folder on Realme Phone? Mar 23, 2024 pm 02:30 PM

Title: Realme Phone Beginner’s Guide: How to Create Folders on Realme Phone? In today's society, mobile phones have become an indispensable tool in people's lives. As a popular smartphone brand, Realme Phone is loved by users for its simple and practical operating system. In the process of using Realme phones, many people may encounter situations where they need to organize files and applications on their phones, and creating folders is an effective way. This article will introduce how to create folders on Realme phones to help users better manage their phone content. No.

How to use Xiaomi Auto app How to use Xiaomi Auto app Apr 01, 2024 pm 09:19 PM

Xiaomi car software provides remote car control functions, allowing users to remotely control the vehicle through mobile phones or computers, such as opening and closing the vehicle's doors and windows, starting the engine, controlling the vehicle's air conditioner and audio, etc. The following is the use and content of this software, let's learn about it together . Comprehensive list of Xiaomi Auto app functions and usage methods 1. The Xiaomi Auto app was launched on the Apple AppStore on March 25, and can now be downloaded from the app store on Android phones; Car purchase: Learn about the core highlights and technical parameters of Xiaomi Auto, and make an appointment for a test drive. Configure and order your Xiaomi car, and support online processing of car pickup to-do items. 3. Community: Understand Xiaomi Auto brand information, exchange car experience, and share wonderful car life; 4. Car control: The mobile phone is the remote control, remote control, real-time security, easy

How to use Dewu installment purchase How to use Dewu installment purchase Mar 24, 2024 pm 01:46 PM

How to use Dewu installment purchase? You can use the installment payment service to purchase your favorite goods in Dewu APP. Most people don’t know how to use Dewu installment purchase. Next is the Dewu installment purchase brought by the editor to users. Graphic tutorial on how to use it. Interested users can come and take a look! Dewu usage tutorial How to use Dewu installment purchase 1. First open Dewu APP and enter the main page, select your favorite product and enter the purchase page; 2. Then on the product purchase page in the picture below, click [Buy Now] in the lower right corner; 3 , then select the appropriate code number and click the price on the left; 4. Then on the order confirmation page, select [Submit Order] in the lower right corner; 5. Finally, on the payment page, check the button behind [Huabei Installment]. Just select the installment type and you’re done

See all articles