Introduction to XML Programming

Source: Internet
Author: User
Tags query relative return
Xml| programming

1. XML Document Manipulation
1.1 Loading an XML document
Dim Objdoc as New XmlDocument ()
'--Load XML document Sample.xml
Objdoc.load ("Sample.xml")

1.2 Loading an XML data
Dim Objdoc as New XmlDocument ()
Dim Strxml as String
Strxml = "<employees><employee id=" 12345 "><Employee_ID>12345</Employee_ID><Name> Zhang bin</name></employee></employees> "

'--Load XML data
Objdoc.load (Strxml)

1.3 Save the document
'-save XML document
Objdoc.save ("Sample.xml")

2 Query for XML data
The most common types of XML data are: Element, Attribute,comment, Text.
An Element, as a <Name>Tom<Name> node. It can include: Element, Text, Comment, ProcessingInstruction, CDATA, and EntityReference.
Attribute refers to the bold part of the <employee id= "12345" >.
Comment,:<!--the node of my Comment-->.
Text refers to the bold part of <Name>Tom<Name>.
In XML, you can use XmlNode objects to refer to various types of XML data.
2.1 node (set) that queries known absolute paths
objNodeList = Objdoc.selectnodes ("Company/department/employees/employee")
Or
objNodeList = Objnode.selectnodes ("/company/department/employees/employee")
Both of these methods return a NodeList object, and if you want to return a single node, you can use the selectSingleNode method, which, if queried to one or more nodes, returns the first node, or none of the nodes without the query returns nothing. For example:
Objnode = Objnode.selectsinglenode ("/company/department/employees/employee")
If not (Objnode are nothing) then
'-Do process
End If
2.2 A node (set) that queries a known relative path
You can query XML data by using a relative path similar to the file path
Objnode = Objdoc.selectsinglenode ("Company/department")
objNodeList = Objnode.selectnodes (".. /department)
Objnode = Objnode.selectnode ("Employees/employee")
2.3 Querying a node (set) of known element names
When using irregular hierarchy documents, because you do not know the middle level element name, you can use the//symbol to cross the middle node, query its child, grandchild or all other elements under the hierarchy. For example:
objNodeList = Objdoc.selectnodes ("Company//employee")
2.4 Query Attribute Node
All of the above methods return elements (Element) nodes (sets), return attributes, only by using the appropriate method, with an @ symbol before the property name, for example:
objNodeList = Objdoc.selectnodes ("company/department/employees/employee/@id")
objNodeList = Objdoc.selectnodes ("company//@id")
2.5 Query Text node
Use text () to get the text node.
Objnode = Objdoc.selectsinglenode ("Company/department/deparmt_name/text ()")
2.6 Nodes that query specific criteria
Use the [] symbol to query the node for a specific condition. For example:
A. Return an employee node with ID number 10102
Objnode = Objdoc.selectsinglenode ("company/department/employees/employee[@id = ' 10102 ']")
B. Return the name node named Zhang Qi
Objnode = Objdoc.selectsinglenode ("company/department/employees/employee/name[text () = ' Zhang Qi '")
C. Return department name node with staff 22345
Objnode = Objdoc.selectsinglenode ("company/department[employees/employee/@id = ' 22345 ']/department_name")
2.7 Querying multiple-mode nodes
Use | A symbol can obtain a node of multiple patterns. For example:
objNodeList = Objdoc.selectnodes ("company/department/department_name | Company/department/manager ")
2.8 Querying any child nodes
Use the * symbol to return all child nodes of the current node.
objNodeList = Objdoc.selectnodes ("Company/*/manager")
Or
objNodeList = Objnode.childnodes

3 Editing of XML data
3.1 Adding an attribute node to an element
Dim Objnodeattr as XmlNode
Objnodeattr = Objdoc.createattribute ("id", nothing)
Objnodeattr.innerxml = "101"
ObjNode.Attributes.Append (objnodeattr)
3.2 Delete an element's properties
ObjNode.Attributes.Remove (objnodeattr)
3.3 Add a child element
Dim Objnodechild as XmlNode
Objnodechild = Objdoc.createelement (Nothing, "IDs", nothing)
Objnodechild.innerxml = "101"
Objnode.appendchild (Objnodechild)
3.4 Delete a child element
Objnode.removechild (Objnodechild)
3.5 replacing a child element
Objnode.replacechild (Newchild,oldchild)

4 Reference data
<?xml version= "1.0" encoding= "UTF-8"?>
<Company>
<department id= ">"
<department_name>cai wubu</department_name>
<manager>zhang bin</manager>
<Employees>
<employee id= "12345" >
<Employee_ID>12345</Employee_ID>
<name>zhang bin</name>
<Gender>male</Gender>
</Employee>
<employee id= "10101" >
<Employee_ID>10101</Employee_ID>
<name>zhang qi</name>
<Gender>female</Gender>
</Employee>
<employee id= "10102" >
<Employee_ID>10102</Employee_ID>
<name>zhang xia</name>
<Gender>male</Gender>
</Employee>
<employee id= "10201" >
<Employee_ID>10201</Employee_ID>
<Name>ZhangChuang</Name>
<Gender>male</Gender>
</Employee>
<employee id= "10202" >
<Employee_ID>10202</Employee_ID>
<name>zhang jun</name>
<Gender>male</Gender>
</Employee>
</Employees>
</Department>
<department id= "102" >
<department_name>kaifa bu</department_name>
<manager>wang bin</manager>
<Employees>
<employee id= "22345" >
<Employee_ID>22345</Employee_ID>
<name>wang bin</name>
<Gender>male</Gender>
</Employee>
<employee id= "20101" >
<Employee_ID>20101</Employee_ID>
<name>wang qi</name>
<Gender>female</Gender>
</Employee>
<employee id= "20102" >
<Employee_ID>20102</Employee_ID>
<name>wang xia</name>
<Gender>male</Gender>
</Employee>
<employee id= "20201" >
<Employee_ID>20201</Employee_ID>
<name>wang chuang</name>
<Gender>male</Gender>
</Employee>
<employee id= "20202" >
<Employee_ID>20201</Employee_ID>
<name>wang jun</name>
<Gender>male</Gender>
</Employee>
</Employees>
</Department>
</Company>



Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.