In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to use php to operate xml". In daily operation, I believe many people have doubts about how to use php to operate xml. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to use php to operate xml". Next, please follow the editor to study!
Data to be manipulated
The copy code is as follows:
David Flanagan
Luke Welling
Laura Thomson
David Courley
Brian Totty
Several basic Concepts of XML
1. Node: node is the Node when dealing with XML in many programming languages. Node is a relatively broad concept. In XML, elements, attributes, namespaces, comments, text content, processing instructions, and the whole document belong to the node, that is to say, each independent small part of the XML document is a node, yes, also, name= "XXXX", and the tag is Even the author's name David Flanagan is a text node.
2, elements: many program languages have to deal with XML, node is a very broad concept, because to unify API, there will not be too many methods for nodes, and elements, that is, Element is a subset of nodes, simply speaking, it is such a label, generally there will be a lot of operation methods for elements.
3. Attribute: this is easy to understand. Things like XX= "OO" are attribute nodes.
4. Escape characters: similar to HTML, xml also has symbols occupied by language. When you want to use these special characters, you need to escape them.
DOMDocument object
I use the DOMDocument object to manipulate xml, which feels a little more scientific than simpleXml. Of course, using php on the first day is purely personal. DOMDocument has several commonly used properties and methods.
Load xml
The copy code is as follows:
$path=$_SERVER ["DOCUMENT_ROOT"].'/ books.xml'
$books=new DOMDocument ()
$books- > load ($path)
Read / traverse nodes and attributes
The copy code is as follows:
$bookElements=$books- > getElementsByTagName ('book')
Foreach ($bookElements as $book) {
Foreach ($book- > attributes as $attr) {
Echo strtoupper ($attr- > nodeName).'-. $attr- > nodeValue.''
}
Echo "AUTHOR:"
Foreach ($book- > getElementsByTagName ('author') as $author) {
Echo $author- > nodeValue.' '
}
Echo''
}
Of course, for many attributes, you only want to read one, which can be read by index through the item (index) method.
The copy code is as follows:
Echo $book- > attributes- > item (1)-> nodeValue
You can also use the powerful xpath query
The copy code is as follows:
You can also use the powerful xpath query
Modify attributes / nodes
The copy code is as follows:
Foreach ($bookElements as $book) {
Foreach ($book- > attributes as $attr) {
# $book- > setAttribute ($attr- > nodeName,strtoupper ($attr- > nodeValue))
$attr- > nodeValue=strtoupper ($attr- > nodeValue)
}
Echo "AUTHOR:"
Foreach ($book- > getElementsByTagName ('author') as $author) {
$author- > nodeValue=strtoupper ($author- > nodeValue)
}
}
$books- > save ($path)
You can directly access the nodeValue changes to the property changes, or you can use the setAttribute method, and don't forget to use save to save the changes.
The copy code is as follows:
$book- > setAttribute ($attr- > nodeName,strtoupper ($attr- > nodeValue))
$attr- > nodeValue=strtoupper ($attr- > nodeValue)
Add elements / attributes
The copy code is as follows:
$newBook=$books- > createElement ('book'); # create a new element
$newBook- > setAttribute ('name','PHP Objects, Patterns, and Practice'); # create a new property, method 1
$publisher=$books- > createAttribute ('publisher'); # create a new property, method 2
$publisher- > nodeValue='Apress L.P'
$newBook- > appendChild ($publisher); # add attributes to the element
$author=$books- > createElement ('author'); # create child elements
$author- > nodeValue='Matt Zandstra'
$newBook- > appendChild ($author); # add child elements to the parent element
$books- > documentElement- > appendChild ($newBook); # add the entire node
$books- > save ($path)
Delete attribute / Node
The copy code is as follows:
$first=$bookElements- > item (0)
$first- > removeAttribute ('publisher')
$second=$bookElements- > item (1)
$second- > parentNode- > removeChild ($second)
$books- > save ($path)
At this point, the study on "how to use php to operate xml" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.