Different tastes-parsing, formatting, and traversing Html and Xml

Source: Internet
Author: User

For more information, see the official website: http://www.tinygroup.org

TinyXmlParser is simple, practical, and fast.

Example 1: Xml string parsing

For example, you can parse an Xml string as follows:

XmlDocument xmlDocument = new XmlStringParser().parse("a");
Example 2: output formatted Xml:
XmlFormater formater = new XmlFormater();System.out.println(formater.format(xmlDocument));

The running result is as follows:

  a

Of course, you can use another method, such as the following:

XmlDocument xmlDocument = new XmlStringParser().parse("a");XmlFormater formater = new XmlFormater();formater.format(xmlDocument,System.out));
The output result is the same as above. Example 3: Chinese tags supported
XmlDocument document = new XmlStringParser (). parse ("Aaa
 <中>
  
Citic
 "); Document. write (System. out); XmlFormater formater = new XmlFormater (); formater. format (document, System. out );

In the preceding example, two methods are used: one is non-formatting output, one is output by default, and the other is formatting output:

Aaa
 <中>
  
Citic
       Aaa    
 <中>
  
Citic
   

We can see that Chinese labels and attributes are also perfectly supported.

Example 4: Fault Tolerance example
XmlDocument document = new XmlStringParser().parse("a");XmlFormater formater = new XmlFormater();formater.format(document, System.out);

In the above example, there is no end tag.

The output result is as follows:

  a

We can see that it has done its best to guess whether it is correct.

Example 5: Performance Test
XmlNode node = null;public NameFilterTest() {node = new XmlNode("root");for (int i = 0; i < 60; i++) {XmlNode a = node.addNode(new XmlNode("a" + i));for (int j = 0; j < 60; j++) {XmlNode b = a.addNode(new XmlNode("b" + j));for (int k = 0; k < 60; k++) {b.addNode(new XmlNode("c" + k));}}}}

Build such a big Dom tree

Long t21 = System. currentTimeMillis (); FastNameFilter
 
  
Fast = new FastNameFilter (node); long t22 = System. currentTimeMillis (); System. out. println ("initialization time:" + (t22-t21); long t1 = System. currentTimeMillis (); for (int x = 0; x <10000; x ++) {XmlNode node = fast. findNode ("b6");} // System. out. println (nodeName); long t2 = System. currentTimeMillis (); System. out. println ("Traversal Time:" + (t2-t1 ));
 

The running result is as follows:

Initialization time: 48 Traversal Time: 20

Note that the above time unit is not minute, not second, but millisecond.

Example 6: node Filtering

It is most important to filter nodes conveniently. This function is too powerful. Therefore, it cannot be demonstrated by examples. Direct paste interface:

Public interface NodeFilter
 
  
> {/*** Initialize the node ** @ param node */void init (T node);/*** sets the attributes that must be included and the values of the corresponding attributes, the ** @ param includeAttributes */void setIncludeAttribute (Map
  
   
IncludeAttributes);/*** sets the attributes that must be included and the values of the corresponding attributes. The ** @ param key * @ param value */void setIncludeAttribute (String key, string value);/*** set the attributes that must be included ** @ param includeAttribute */void setIncludeAttributes (String... includeAttribute);/*** sets the attribute that must be excluded and the corresponding attribute value if it contains the attribute, but the attribute value is different from that in Map, this attribute is allowed. If the attribute contains the same value as that in Map, the attribute ** @ param excludeAttribute */void setExcludeAttribute (Map
   
    
ExcludeAttribute);/*** sets attributes that must be excluded. The specified attribute cannot exist ** @ param excludeAttribute */void setExcludeAttribute (String... excludeAttribute);/*** set the content that must be included. You only need to include this value in the context. ** @ param includeText */void setIncludeText (String... includeText);/*** set excluded content ** @ param excludeText */void setExcludeText (String... excludeText);/*** set the child nodes that must be included ** @ param includeNode */void setIncludeNode (String... includeNode);/*** set the name of a node not allowed by the parent node ** @ param excludeByNode */void setExcludeByNode (String... excludeByNode);/*** set the name of the node that the parent node must contain ** @ param includeByNode */void setIncludeByNode (String... includeByNode);/*** sets the child nodes that must be excluded ** @ param excludeNode */void setExcludeNode (String... excludeNode);/*** set to contain at least one specified node ** @ param xorSubNode */void setXorSubNode (String... xorSubNode);/*** set to contain at least one specified name attribute ** @ param xorProperties */void setXorProperties (String... xorProperties);/*** clear filter condition */void clearCondition ();/*** set the name of the node to search */void setNodeName (String nodeName ); /*** query the List of nodes with the specified node name and other conditions ** @ param nodeName * @ return */List
    
     
FindNodeList (String nodeName);/*** searches for nodes based on their names and other conditions. If multiple nodes exist, only the first ** @ param nodeName * node name to be searched * @ return */T findNode (String nodeName) is returned ); /*** search for the nodes that match the set node name. If there are multiple nodes, only the first ** @ return */T findNode () found is returned (); /*** search for the node List that matches the set node name ** @ return */List
     
      
FindNodeList ();}
     
    
   
  
 

That is to say, it supports filtering by the specified attribute name and attribute value on the node (multiple groups can be specified), and specifying the attribute name (no matter what the value is, you can specify multiple), you can specify the exclusion attribute and attribute value (that is, the attribute name and value that cannot be included, can contain multiple groups), the attribute that cannot be included (can contain multiple groups), and the text content (you can specify multiple groups) file Content that cannot be included (multiple groups can be specified), including the node name (multiple groups can be specified), and nodes that cannot be included (multiple groups can be specified) you can specify a node (multiple groups can be specified), a node (multiple groups can be specified), or a node that contains at least one of the nodes, you can specify one of the following attributes and search by node name.

All the above conditions can be combined for search.

After talking about this, let's look at the test cases:

node = new XmlNode("root");XmlNode n1 = node.addNode(new XmlNode("aa"));n1.setAttribute("a", "av");n1.setAttribute("b", "bv");n1.addNode(new XmlNode("a"));n1 = node.addNode(new XmlNode("aa"));n1.setAttribute("a", "av1");n1.setAttribute("b", "bv1");n1.setAttribute("c", "cv1");n1.addNode(new XmlNode("b"));

A Dom tree is built above:

                   
        
 

Below are a bunch of test cases:

Filter = new NameFilter (node); filter. clearCondition (); assertEquals (1, filter. findNodeList ("root "). size (); filter. setExcludeAttribute ("c"); assertEquals (1, filter. findNodeList ("aa "). size (); // test the inclusion attribute name filter. clearCondition (); assertEquals (1, filter. findNodeList ("root "). size (); filter. setIncludeAttributes ("c"); assertEquals (1, filter. findNodeList ("aa "). size (); // The test contains the specified attribute value filter. clearCondition (); Hashtable
 
  
Pht = new Hashtable
  
   
(); Pht. put ("a", "av1"); filter. setIncludeAttribute (pht); assertEquals (1, filter. findNodeList ("aa "). size (); filter. setExcludeAttribute ("c"); assertEquals (0, filter. findNodeList ("aa "). size (); // The test contains the specified node filter. clearCondition (); filter. setIncludeNode ("a"); assertEquals (1, filter. findNodeList ("aa "). size (); filter. setIncludeAttributes ("c"); assertEquals (0, filter. findNodeList ("aa "). size (); // The test contains the specified node filter. clearCondition (); filter. setExcludeNode ("c"); assertEquals (2, filter. findNodeList ("aa "). size ());
  
 

The test case is ugly, but a simple demonstration of its use is provided.

In all the above examples, converting X into Ht is for the Html Parser. The API is completely consistent and the usage is completely the same.

The difference is that labels and attribute names in Xml are case-sensitive, while Html is case-insensitive.

In addition, Html supports single tags.

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.