Tinyxmlparser open source, huh?

Source: Internet
Author: User
Tags xml parser

Pros: Efficient, simple, and easy-to-use XML parser.
Study time, minutes.
Support Chinese tag name and attribute name, support underline, minus sign and so on separator.
Resolution faster than, Find Fast, support format.
Disadvantage: XML SCHEMA,DTD checksum is not supported.
MAVEN Reference Coordinates:

1
2
3
4
5
<dependency>
< groupid>org.tinygroup</groupid>
< artifactid>xmlparser</artifactid>
< version>0.0.12</version>
</dependency>



Parse the following XML


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version= "1.0"?>
< students>
<student>
<name>John</name>
<grade>B</grade>
<age>12</age>
</student>
<student>
<name>Mary</name>
<grade>A</grade>
<age>11</age>
</student>
<student>
<name>Simon</name>
<grade>A</grade>
<age>18</age>
</student>
</students>



Example code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
public class Testxmlparser {
public static void Main (string[] args) throws Throwable {
File File = new file ("E:/test/students.xml");
Xmlstringparser parser = new Xmlstringparser ();
XmlDocument document = Parser.parse (Ioutils.readfrominputstream (
New FileInputStream (file), "Utf-8");
Printstudents (Document.getroot ());
}
private static void Printstudents (XmlNode studentsnode) {
For (XmlNode studentNode:studentsNode.getSubNodes ("student")) {
Printstuent (Studentnode);
}
}
private static void Printstuent (XmlNode studentnode) {
Printsubtagbyname (Studentnode, "name");
Printsubtagbyname (Studentnode, "grade");
Printsubtagbyname (Studentnode, "age");
}
private static void Printsubtagbyname (XmlNode studentnode,string tagName) {
System.out.println (Studentnode.getsubnode (tagName). GetContent ());
}
}



Examples of formatting:

1
2
3
4
5
XmlDocument Doc;
doc = new Xmlstringparser ()
. Parse ("Xmlformater f = new Xmlformater ();
System.out.println (F.format (DOC));



Operation Result:

1
2
3
4
5
6
7
<title>
Aaa
</title>



Performance testing: Build the following node size:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
htmlnode node = null;

    public namefiltertest () {
        node = new Htmlnode ("root");
        for (int i = 0; i < i++) {
           & nbsp Htmlnode a = Node.addnode (new Htmlnode ("a" + i));
            for (int j = 0; J <; J + +) {
     &NBSP;&NB Sp         Htmlnode b = A.addnode (new Htmlnode ("B" + j));
                for (int k = 0; k <; k++) {
  &NBSP;&N Bsp                b.addnode (New Htmlnode ("C" + K));
               }
            }
        }
   }



That is, when the number of nodes 60+60*60+60*60*60 the number of nodes, the following lookup:

1
2
3
4
5
6
7
8
9
10
11
Long t21 = System.currenttimemillis ();
Fastnamefilter fast = new Fastnamefilter (node);
Long t22 = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("Fast Initialization Time" + (t22-t21));
Long T1 = System.currenttimemillis ();
String nodeName = null;
for (int x = 0; x < 10000; × x + +) {
NodeName = Fast.findnode ("B6"). Getnodename ();
}
Long t2 = System.currenttimemillis ();
System.out.println ("Fastnamefilter Time" + (T2-T1));



Operation Result:

1
2
Fast Initialization spents 130
Fastnamefilter spents 39



that is, at 219,661 node size, find the specified node 10,000 times, only 39ms, there is faster than this?     If so far, there is nothing, it provides filtering capabilities to meet the vast majority of the application scenario, first look at the interface:  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
Public interface Nodefilter<t extends node<t>> {
/**
* Initialize Node
*
* @param node
*/
void init (T node);

/**
* Set the properties that must be included and the value of the corresponding property, must exist
*
* @param includeattributes
*/
void Setincludeattribute (map<string, string> includeattributes);

/**
* Set the properties that must be included and the value of the corresponding property, must exist
*
* @param key
* @param value
*/
void Setincludeattribute (String key, String value);

/**
* Set properties that must be included
*
* @param includeattribute
*/
void Setincludeattributes (String ... includeattribute);

/**
* Set properties and corresponding property values that must be excluded if the attribute is included, but the value of the property is not the same as in map, the property is allowed to exist if it contains a property and the value of the property is the same as in map.
*
* @param excludeattribute
*/
void Setexcludeattribute (map<string, string> excludeattribute);

/**
* Set the properties that must be excluded, the specified property cannot exist
*
* @param excludeattribute
*/
void Setexcludeattribute (String ... excludeattribute);

/**
* Set the content that must be included, only the package include that value in the context.
*
* @param includetext
*/
void Setincludetext (String ... includetext);

/**
* Set the content that must be excluded
*
* @param excludetext
*/
void Setexcludetext (String ... excludetext);

/**
* Set child nodes that must be included
*
* @param includenode
*/
void Setincludenode (String ... includenode);

/**
* Set the node name not allowed by the parent node
*
* @param excludebynode
*/

void Setexcludebynode (String ... excludebynode);

/**
* Set the node name that the parent node must contain
*
* @param includebynode
*/
void Setincludebynode (String ... includebynode);

/**
* Set child nodes that must be excluded
*
* @param excludenode
*/

void Setexcludenode (String ... excludenode);

/**
* Set a node that contains at least one specified name
*
* @param xorsubnode
*/
void Setxorsubnode (String ... xorsubnode);

/**
* Set contains at least one specified name property
*
* @param xorproperties
*/
void Setxorproperties (String ... xorproperties);

/**
* Clear Filter conditions
*/
void Clearcondition ();

/**
* Set the name of the node to search
*/
void Setnodename (String nodeName);

/**
* Find the name of the specified node and a list of nodes that meet other criteria
*
* @param nodeName
* @return
*/
List<t> findnodelist (String nodeName);

/**
* Find nodes based on name and other criteria, and if there are multiple, return only the first one
*
* @param nodeName
* The name of the node to find
* @return
*/
T FindNode (String nodeName);

/**
* Search for nodes that meet the node name of the set, and if there are multiple, only the first one found is returned
*
* @return
*/
T FindNode ();

/**
* Search for a node list that matches the node name of the setting
*
* @return
*/
List<t> findnodelist ();
}





From the interface above, it can be seen that it supports attribute and attribute value filtering, supports attribute name filtering, supports exclusion name filtering, contains text filtering, contains node name filtering, Filter by node name, excludes sub-node name filtering, contains at least one filter of nodes names, contains at least one attribute filter, section call filter, These filter conditions can be used in combination.
With such a powerful node filtering feature, programmers can use XML more easily and easily.

Tinyxmlparser Open Source

Related Article

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.