Streaming XPath evaluation

后端 未结 7 1391
萌比男神i
萌比男神i 2020-12-13 21:06

Are there any production-ready libraries for streaming XPath expressions evaluation against provided xml-document? My investigations show that most of existing solutions loa

7条回答
  •  执念已碎
    2020-12-13 21:35

    I think I'll go for custom code. .NET library gets us quite close to the target, if one just wants to read some paths of the xml document.

    Since all the solutions I see so far respect only XPath subset, this is also this kind of solution. The subset is really small though. :)

    This C# code reads xml file and counts nodes given an explicit path. You can also operate on attributes easily, using xr["attrName"] syntax.

      int c = 0;
      var r = new System.IO.StreamReader(asArgs[1]);
      var se = new System.Xml.XmlReaderSettings();
      var xr = System.Xml.XmlReader.Create(r, se);
      var lstPath = new System.Collections.Generic.List();
      var sbPath = new System.Text.StringBuilder();
      while (xr.Read()) {
        //Console.WriteLine("type " + xr.NodeType);
        if (xr.NodeType == System.Xml.XmlNodeType.Element) {
          lstPath.Add(xr.Name);
        }
    
        // It takes some time. If 1 unit is time needed for parsing the file,
        // then this takes about 1.0.
        sbPath.Clear();
        foreach(object n in lstPath) {
          sbPath.Append('/');
          sbPath.Append(n);
        }
        // This takes about 0.6 time units.
        string sPath = sbPath.ToString();
    
        if (xr.NodeType == System.Xml.XmlNodeType.EndElement
            || xr.IsEmptyElement) {
          if (xr.Name == "someElement" && lstPath[0] == "main")
            c++;
          // And test simple XPath explicitly:
          // if (sPath == "/main/someElement")
        }
    
        if (xr.NodeType == System.Xml.XmlNodeType.EndElement
            || xr.IsEmptyElement) {
          lstPath.RemoveAt(lstPath.Count - 1);
        }
      }
      xr.Close();
    

提交回复
热议问题