XML解析查询Linq以搜索所需

时间:2018-06-27 07:38:13

标签: java

var sqlite3 = require('sqlite3').verbose(); 
var path = require('path');
var Promise = require('bluebird');
var _ = require('underscore')

module.exports.myModule = function(input){

  //the copy-from database
  var dbTarget = new sqlite3.Database(path.join(__dirname, '/db/myDB.db'));  

   // the copy-to database
   var dbSource = new sqlite3.Database(input.path); 

   // selection query that determines what to copy from the source db
    var selectionQuery = "SELECT colA, colB  FROM sourceTable WHERE colB > 0" 

    dbTarget.serialize(function() {

      // fetches all source rows
      dbSource.all(selectionQuery, function(err, rows) {

        // for each row fetched from the sourceDB
       _.each(rows,function (row) {

         // insert into targetDB if unique else ignore and move onto the next line
         var str = "INSERT OR IGNORE INTO `mytable` (COL_A,COL_B) VALUES(?,?)";

         //per the https://github.com/mapbox/node-sqlite3 documentation ( prepare statement then execute)
         var stmt = dbTarget.prepare(str);

         stmt.run(input.valA,input.valB,function(err) {
           if(err) {throw err; }
         });
         stmt.finalize();
       })
    })
  });
};

同样,我需要对每个火炬名称节点执行此操作。

请帮助我完成此任务

2 个答案:

答案 0 :(得分:0)

XmlDocument doc = new XmlDocument();
doc.LoadXml(XMLSTRING);

foreach (XmlNode node in doc.DocumentElement.ChildNodes)
    {
        for (int i = 0; i < 3; i++)
        {
            node.ChildNodes[i]......
        }
    }

答案 1 :(得分:0)

使用Regex和xml linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var groups = doc.Descendants("GLOBALLIST").Select(x => new {
                name = (string)x.Attribute("name"),
                items = x.Elements("LISTITEM")
                  .Select(y => (string)y.Attribute("value"))
                  .Select(y => y.Split(new char[] { '/' }))
                  .Select(y => new { path = y.FirstOrDefault(), name = y.LastOrDefault() })
                  .ToList()
            }).ToList();

            var results = groups.Select(x => new
            {
                name = x.name,
                items = x.items
                    .GroupBy(y => y.path)
                    .Select(y =>
                       y.OrderByDescending(z => GetNumber(z.name))
                       .Take(3).ToList())
                    .ToList()
            }).ToList();
        }

        static int? GetNumber(string input)
        {
            string pattern = @"\d+$";
            string strNumber = Regex.Match(input, pattern).Value;

            int number = 0;
            bool results = int.TryParse(strNumber, out number);
            if (results == false) return null;
            return number;
        }
    }
}