Home Backend Development PHP Tutorial A very handy XML class! ! Original_PHP Tutorial

A very handy XML class! ! Original_PHP Tutorial

Jul 21, 2016 pm 04:11 PM
xml Original Thought of kind want


// I am not familiar with XML, and the description of XML may be wrong in the annotation
// This is just an idea. To be implemented, it should be slightly modified. Some data that are not commonly queried using XML can be used. Save
// For example, user information in a virtual community will generally be displayed only if the user himself or another user specifies his USERNAME
// The rest requires opening, querying, interpreting data sets, and closing the database every time consumption.

define("enter",chr(13).chr(10));
class DATA_XML //Key! ! ! A custom class
{
var $parser; //XML interpreter
var $tags; //XML tags
var $on; //
var $root; // root Element
var $Data = array(); // Element array

function DATA_XML($filename,$root) //Class initial function filename file name, root root element
{
$this->root = $root; //Initial root element
$this->parser = xml_parser_create();//Create interpretation object
xml_set_object(&$this->parser,&$this );//Set object
xml_set_element_handler($this->parser,"tag_on","tag_off");//Set element management function
xml_set_character_data_handler($this->parser,"getdata"); //Set data management events
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); //Requires strict case sensitivity

if( file_exists($filename) ) //File found?
{//Find
$fp = fopen($filename,"r"); //Open for read only
$c = fread($fp,filesize($filename)); //Read data
fclose($fp);//Close
$this->parse($c); //Set the interpretation object
}
else
{
echo "ooooo "; //! ? ! It seems to be used for debugging, just remove the else
}


}

function parse($data)//Explanation function
{
xml_parse ($this->parser,$data);
}

function tag_on($parser,$tag,$attributes)//Discover element trigger function
{
$this- >on = true; // Already triggered
$this->tags = $tag;
//echo $tag;
}

function tag_off($parser,$ tag) //Element binding function
{
$this->on = false; // Element binding
}

function getdata($parser,$cdata) // Take out the element
{
if($this->on && $this->tags!=$this->root)
{//It is better to verify, (whether it is an element now starting, and not the root element? )

$this->Data[$this->tags]=trim($cdata);
                                                ! Use tags to make an array table and add data to the array elements
//echo trim($cdata);
//echo $this->tags;
//echo "n";
}
}
function check($str)
{//Check character
if( strlen($str)<1)//If it is a null character
return ' ' ;//Return ' '
else
return $str;//Otherwise return to the original path (otherwise the XML will not seem to have an end tag)
}
function saveas($filename)//Save as For
{
$c=''.enter; ",chr(13).chr(10));
$c.="<".$this->root.">".enter;
for( reset($this->Data);$i=key($this->Data);next($this->Data))
{//Start to list all the data
$c.= "<".$i.">".$this->check($this->Data[$i])."// Use the array table name as the element label and add the data content
}
//echo $c;
$c.="root.">".enter;//End tag
$fp = fopen( $filename , "w" );//Write file
fwrite($fp,$c);
fclose($fp);

}
};




/*New key an XML file
                                                      DATA_XML(path."arm.xml","DATA"/*root element*/);
$xml->Data["UserName"]="Guan Yu";
    $xml->Data[ "Nick"]="Guan Yunchang";
$xml->saveas(path."arm.xml");
unset( $xml );
// Read a file
$ xml = new DATA_XML(path."arm.xml","DATA");
                      echo $xml->Data["Nick"];//Show it?
$xml->Data["Nick"]="Master Guan";//UPDATE
$xml->Data["Master"]="Liu Bei";//INSERT INTO
$ xml->saveas(path."arm.xml");//Save as overwrite itself
*/
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313924.htmlTechArticle? // I am not familiar with XML. The description of XML may be wrong. // This is just an idea. If implemented, it should be slightly modified, and some data queried by uncommon conditions can be saved in XML //...
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

How to set up the keyboard boot function on a GIGABYTE motherboard (enable keyboard boot mode on GIGABYTE motherboard) How to set up the keyboard boot function on a GIGABYTE motherboard (enable keyboard boot mode on GIGABYTE motherboard) Dec 31, 2023 pm 05:15 PM

How to set up keyboard startup on Gigabyte's motherboard. First, if it needs to support keyboard startup, it must be a PS2 keyboard! ! The setting steps are as follows: Step 1: Press Del or F2 to enter the BIOS after booting, and go to the Advanced (Advanced) mode of the BIOS. Ordinary motherboards enter the EZ (Easy) mode of the motherboard by default. You need to press F7 to switch to the Advanced mode. ROG series motherboards enter the BIOS by default. Advanced mode (we use Simplified Chinese to demonstrate) Step 2: Select to - [Advanced] - [Advanced Power Management (APM)] Step 3: Find the option [Wake up by PS2 keyboard] Step 4: This option The default is Disabled. After pulling down, you can see three different setting options, namely press [space bar] to turn on the computer, press group

The first choice for CS players: recommended computer configuration The first choice for CS players: recommended computer configuration Jan 02, 2024 pm 04:26 PM

1. Processor When choosing a computer configuration, the processor is one of the most important components. For playing games like CS, the performance of the processor directly affects the smoothness and response speed of the game. It is recommended to choose Intel Core i5 or i7 series processors because they have powerful multi-core processing capabilities and high frequencies, and can easily cope with the high requirements of CS. 2. Graphics card Graphics card is one of the important factors in game performance. For shooting games such as CS, the performance of the graphics card directly affects the clarity and smoothness of the game screen. It is recommended to choose NVIDIA GeForce GTX series or AMD Radeon RX series graphics cards. They have excellent graphics processing capabilities and high frame rate output, and can provide a better gaming experience. 3. Memory power

Digital audio output interface on the motherboard-SPDIF OUT Digital audio output interface on the motherboard-SPDIF OUT Jan 14, 2024 pm 04:42 PM

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

How to handle XML and JSON data formats in C# development How to handle XML and JSON data formats in C# development Oct 09, 2023 pm 06:15 PM

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

Glodon Software's computer configuration recommendations; Glodon Software's computer configuration requirements Glodon Software's computer configuration recommendations; Glodon Software's computer configuration requirements Jan 01, 2024 pm 12:52 PM

Glodon Software is a software company focusing on the field of building informatization. Its products are widely used in all aspects of architectural design, construction, and operation. Due to the complex functions and large data volume of Glodon software, it requires high computer configuration. This article will elaborate on the computer configuration recommendations of Glodon Software from many aspects to help readers choose a suitable computer configuration processor. Glodon Software requires a large amount of data calculation and processing when performing architectural design, simulation and other operations. Therefore, the requirements for the processor are higher. It is recommended to choose a multi-core, high-frequency processor, such as Intel i7 series or AMD Ryzen series. These processors have strong computing power and multi-thread processing capabilities, and can better meet the needs of Glodon software. Memory Memory is affecting computing

How to use PHP functions to process XML data? How to use PHP functions to process XML data? May 05, 2024 am 09:15 AM

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

Convert POJO to XML using Jackson library in Java? Convert POJO to XML using Jackson library in Java? Sep 18, 2023 pm 02:21 PM

Jackson is a Java-based library that is useful for converting Java objects to JSON and JSON to Java objects. JacksonAPI is faster than other APIs, requires less memory area, and is suitable for large objects. We use the writeValueAsString() method of the XmlMapper class to convert the POJO to XML format, and the corresponding POJO instance needs to be passed as a parameter to this method. Syntax publicStringwriteValueAsString(Objectvalue)throwsJsonProcessingExceptionExampleimp

See all articles