拓展阅读
config 配置方式概览-8 种配置文件介绍对比 xml/json/proeprties/ini/yaml/TOML/hcl/hocon
config HCL(HashiCorp Configuration Language) 配置文件介绍
config HCL(HashiCorp Configuration Language) 官方文档翻译
config HOCON(Human-Optimized Config Object Notation)配置文件介绍
XStream java 实现 xml 与对象 pojo 之间的转换
java 实现 xml 与对象 pojo 之间的转换的几种方式 dom4j/xstream/jackson
YAML-02-yml 配置文件 java 整合使用 yamlbeans + snakeyaml + jackson-dataformat-yaml
XML
假设我们要解析形如 manifest.xml
的文件。
- manifest.xml
<manifest>
<templetes>
<table name="mdm.md_Bond">
<map field="UID" pos="2" />
<map field="UniqueKey" pos="3" />
<map field="LastUpdatedTime" pos="4" />
</table>
<table name="hha.md_Bond">
<map field="UID" pos="3" />
</table>
</templetes>
</manifest>
- XmlService.cs
/// <summary>
/// 获取指定路径下,指定表明对应的数据
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="tableName">对应表名</param>
/// <returns></returns>
public static Dictionary<string, int> GetFieldMapping(string filePath, string tableName)
{
Dictionary<string, int> result = new Dictionary<string,int>();
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode manifest = xmlDoc.SelectSingleNode("manifest");
XmlNode template = manifest.FirstChild;
foreach (XmlElement table in template)
{
string name = table.GetAttribute("name");
if (tableName.Equals(name))
{
foreach (XmlElement map in table)
{
string field = map.GetAttribute("field");
string posStr = map.GetAttribute("pos");
int pos = int.Parse(posStr);
result.Add(field, pos);
}
}
}
}
catch (Exception ex)
{
//throw ex;
return null;
}
return result;
}
- Main()
static void Main(string[] args)
{
string path = @"E:\CODE\xml\manifest.xml";
string tableName = "mdm.md_Bond";
Dictionary<string, int> result = XmlService.GetFieldMapping(path, tableName);
foreach(string key in result.Keys)
{
Console.WriteLine(key + " "+ result[key]);
}
Console.ReadKey();
}
结果
UID 2
UniqueKey 3
LastUpdatedTime 4