PHP learning notes: Use php to read xml files and take notes in xml
Xml has been gradually replaced by json. Currently, APIs are all using json, but some old websites still use xml.
Here, the default xml
This article explains how to parse XML through ElementTree, get the son node, insert the Son node, manipulate the attributes, beautify the XML . 1. The introduction of the library requires 3 classes, elementtree,element, and a wrapper class to create subclasses subelementnbsp; from Xml.etree.ElementTree import ElementTree from Xml.etree.ElementTree Import Element from xml.etree.ElementTree import subelemen
Use javascript to read xml files and read node data. javascriptxml
The following code reads the node data, and read the node attribute data.
Javascript reads xml files
Don't use childNodes [x] to retrieve it. It will make you dizzy and have no idea which node to choose. Use
XML is a shorthand for extensible Markup language, an extensible identity language. Now teach you how to open XML files and software.
The easiest thing to do is to open it with an Excel form. You can also open an XML document with Netscape 6来, and you can also right-click "View Source file", and when you open an
Framework SDK when creating XML. Therefore, understanding and understanding the content in this class library is very necessary for C # programming. Next we will introduce how to use C # to read data in XML. Through this, you will find that, C # uses this class library to implement this function.
III. C # Read
Reading and Writing Extensible Markup Language (XML) in PHP may seem a little scary. In fact, XML and all its related technologies may be terrible, but reading and writing XML with PHP is not necessarily a terrible task. First, you need to learn a little about XML-what it is and what it is used. Then, you need to learn
write HTML as well-formed XML, follow the Extensible Hypertext Markup Language (XHTML) standard of the consortium. All modern browsers are capable of rendering XHTML. Also, you can read XHTML with XML tools and find the data in the document, which is much easier than parsing HTML.
reading XML using the DOM library
T
well-formed XML, follow the Extensible Hypertext Markup Language (XHTML) standard of the consortium. All modern browsers are capable of rendering XHTML. Also, you can read XHTML with XML tools and find the data in the document, which is much easier than parsing HTML.
reading XML using the DOM library
The easiest way
text subnode.
The code for reading the XML file of a book and displaying the content in Dom is shown in List 2.
The script first createsnew DOMdocumentObject,loadMethod to load the library XML into this object. Later, the script usesgetElementsByNameMethod to obtain a list of all elements under the specified name.
InbookIn the node loop, the script usesgetEle
that//errorline and Errorcol are the wrong rows and columns}
If the above steps are not wrong, then congratulations, you have got a complete Qdomdocument object doc, you can only access the child nodes or child nodes of the doc to get the contents of all the tags in the XML.3)You can get all the child node list qdomnodelist of Doc through Doc.childnodes (). Like what
Qdomnodelist list=doc.childnodes (); for (int i=0;i
By the above method you can know
("Administrator"); textWriter. writeElementString ("Name", "formble"); textWriter. writeElementString ("site", "w3sky.com"); textWriter. writeEndElement (); // end of writing a document. call the WriteEndDocument method textWriter. writeEndDocument (); // disable textWriter. close ();} catch (System. exception e) {Console. writeLine (e. toString ());}}}}
Three methods for reading XML documents
Use the XmlTextReader class object to
The simplest case for reading XML in PHP:
The XML file (Cy.xml) is as follows:
Copy CodeThe code is as follows:
1
2
3
The php file (cy.php) is as follows:
Copy CodeThe code is as follows:
$xml = new DOMDocument
, call the WriteEndDocument method.TextWriter. WriteEndDocument ();
// Disable textWriterTextWriter. Close ();}Catch (System. Exception e){Console. WriteLine (e. ToString ());}}
}}
Three methods for reading XML documents
Use the XmlTextReader class object to read the XML document. Specify the location of the XML
Schema file is customer. the xsdconfig file is customer. xsdconfig. in fact, the generated Java source code does not have much effect. what we need is the jar file. let's take a look at the Classes generated under build \ src \ sample \ xmlbean.
CustomersDocument. java -- mertype is mapped to the Java Class of the entire XML
: the entire document tree is in the memory for easy operation. It supports deletion, modification, and re-arrangement among other functions.
Disadvantage: transferring the entire document to memory (often containing a large number of useless nodes) wastes time and space.
Usage: Once the file is parsed, you need to access the data multiple times and have sufficient resources (such as memory and CPU ).
To solve these problems caused by Dom parsing
php file (cy.php) is as follows (and for the first time remains unchanged):
Copy Code code as follows:
$xml = new DOMDocument ();
$xml->load (' cy.xml ');
foreach ($xml->getelementsbytagname (' list ') as $list)
{
$value = $list->firstchild->nodevalue;
echo $value. " }
?>
cy.php Out
()); to } +System.out.println ("===== ends the traversal of a book ====="); - } the}Catch(documentexception e) { * //TODO auto-generated Catch block $ e.printstacktrace ();Panax Notoginseng } - } the}DOM4J reading XML Public classDom4jtest {Private voidCreatexml () {//1. Create a Document object that represents the entire XML documentDocument document =do
1. Save it to the XML file // get the file name to be uploaded by the user
String strFilePathName = loFile. PostedFile. FileName;
String strFileName = Path. GetFileName (strFilePathName );
Int FileLength = loFile. PostedFile. ContentLength;
If (FileLength Return;
Try
{
Byte [] FileByteArray = new Byte [FileLength]; // temporary storage of Byte Arrays for image fi
easy operation. It supports deletion, modification, and re-arrangement among other functions.
Disadvantage: transferring the entire document to memory (often containing a large number of useless nodes) wastes time and space.
Usage: Once the file is parsed, you need to access the data multiple times and have sufficient resources (such as memory and CPU ).
To solve these problems caused by Dom parsing XML
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.