]> wagnertech.de Git - mDoc.git/blob - csharp/mutil/XmlExtractor.cs
pydev-s6git
[mDoc.git] / csharp / mutil / XmlExtractor.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Xml;
4
5 namespace mutil {
6
7 public class XmlExtractor {
8         public const int EC_BEG = 1; // begin of subtree (current node descends) - in
9         public const int EC_CTN = 2; // continue with next sibling element - in / out: data of found element 
10         public const int EC_END = 3; // subtree completion (current node ascends); no data - out
11
12
13         private XmlDocument docNode;
14         private bool textMode;
15         public XmlNode CurrentNode;
16
17         public XmlExtractor(object BaseNode = null, bool TextMode = false) {
18                 this.CurrentNode = (XmlNode)BaseNode;
19                 this.textMode = TextMode;
20         }
21         
22         public void openInput(string input){
23                 this.docNode = new XmlDocument();
24                 this.docNode.Load(input);
25                 this.CurrentNode = this.docNode;
26         }
27         
28         public void toTop(){
29                 this.CurrentNode = this.docNode;
30         }
31         public void toParent(){
32                 this.CurrentNode = this.CurrentNode.ParentNode;
33         }
34         public int extractElement(int extr_ctl, out string elem, out string value, out Dictionary<string,string> attrs) { 
35                 elem  = "";
36                 value = null;
37                 attrs = new Dictionary<string,string>();
38
39                 XmlNode srh_node;
40                 
41                 if (extr_ctl == EC_BEG) {
42                         srh_node = this.CurrentNode.FirstChild;
43                 } else if (extr_ctl == EC_CTN) {
44                         srh_node = this.CurrentNode.NextSibling;
45                 } else {
46                         return EC_END;
47                 }
48
49                 // search for next element node
50                 if (! this.textMode) {
51                         while (srh_node != null) {
52                                 if (srh_node.NodeType != XmlNodeType.Element) {
53                                         srh_node = srh_node.NextSibling;
54                                 } else {
55                                         break;
56                                 }
57                         }
58                 }
59                                 
60                 if (srh_node != null) {
61                         // element node found: extract tag name, value and attributes
62                     this.CurrentNode = srh_node;
63                     this.getCurrentNodeData(out elem, out value, out attrs);
64                         return EC_CTN;
65                 } else {
66                         // terminate / ascend subtree after child extraction
67                         if (extr_ctl == EC_CTN) {
68                                 this.CurrentNode = this.CurrentNode.ParentNode;
69                         }
70                         return EC_END;
71                 }
72                 
73         }
74         public void getCurrentNodeData(out string elem, out string value, out Dictionary<string,string> attrs) {
75             elem = this.CurrentNode.Name;
76                 value = null;
77                 attrs = new Dictionary<string,string>();
78                 if (this.textMode) {
79                         if (this.CurrentNode.NodeType == XmlNodeType.Text) value = this.CurrentNode.Value;
80                 }
81                 else {
82                         if (this.CurrentNode.InnerText != null) value = this.CurrentNode.InnerText;
83                 }
84                 if (this.CurrentNode.Attributes != null) {
85                         for (int att_idx = 0; att_idx < this.CurrentNode.Attributes.Count; att_idx++) {
86                                 var att_node = this.CurrentNode.Attributes[att_idx];
87                                 attrs[att_node.Name] = this.CurrentNode.Attributes[att_idx].Value;
88                         }
89                 }
90         }
91         public XmlNode gotoChild(string name) {
92            // return True/False, if child node could be found
93            int cntl = EC_BEG;
94            string elem;
95            string value;
96            Dictionary<string,string> attrs;
97            while ((cntl = this.extractElement(cntl, out elem, out value, out attrs)) != EC_END) {
98                if (elem == name) return this.CurrentNode;
99            }
100            return null;
101         }
102         public XmlNode requireChild(string name) {
103            // throws exception, if child not present
104            XmlNode child = this.gotoChild(name);
105            if (child == null) throw new Exception("Could not find node "+name);
106            return child;
107         }
108 }
109 }