从XML文件中获取行号 [英] Get line number from XML file

查看:191
本文介绍了从XML文件中获取行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio 2015上使用C ++编写代码并使用Windows API附带的标准XML库。



下面是我拥有的示例XML文件。



 <  文档 >  
< schemaVersion > 1.0.1 < / schemaVersion >
< ID-1 >
< 名称 > 示例XXX < / name >
< / ID-1 >
< ID-2 >
< date > 2009-03-17 < / date >
< ID-2-1 >
< mediaType > myCable < / mediaType >
< / ID-2-1 >
< documentType > myOrder < / documentType >
< / ID-2 >
< ID-3 >
< documentCode > 1 < ; / documentCode >
< ID-3-1 &g t;
< 名称 > kool < / name >
< ID-3-2 >
< id > 555 < / id >
< / ID-3-2 >
< / ID- 3-1 >
< ID-3 >
< / document >





我的任务是找到特定标签的行号。我在网上进行了一些研究,找不到获取行号的方法。



例如,我想得到标签 documentType的行号



我在C ++中没有太多运气。即,我找不到一个可以获取行号的库。目前我正在使用MSXML库。



有人可以指导我是否可以用C ++做到这一点?任何示例代码都会非常有用。



我尝试过:



当前正在循环中递增一个整数,它遍历XML的节点。此整数将保存特定标记的行号。这不是一个正确的逻辑,如果XML文件中有换行符,则会失败。

解决方案

即使是XML文件也是文本文件,因此您可以搜索标签的位置,然后计算CR代码直到标签的位置。



这里有一些示例代码。或者以类似的方式使用查找功能。

Quote:

我找不到一个可以获取行号的库。



原因是行号没有意义。线条和缩进只是为了帮助人类阅读。

从技术上讲,XML可以是单行。这就是为什么没人关心行号。



如果你真的想使用行号,我担心你必须建立自己的解决方案。


I am writing code in C++ on Visual Studio 2015 and using standard XML libs that comes with Windows APIs.

Below is a sample XML file that i have.

<document>
    <schemaVersion>1.0.1</schemaVersion>
    <ID-1>
       <name>Sample XXX</name>
    </ID-1>
    <ID-2>
      <date>2009-03-17</date>
      <ID-2-1>
        <mediaType>myCable</mediaType>
      </ID-2-1>
      <documentType>myOrder</documentType>
    </ID-2>
    <ID-3>
      <documentCode>1</documentCode>
      <ID-3-1>
        <name>kool</name>
        <ID-3-2>
          <id>555</id>
        </ID-3-2>
      </ID-3-1>
    <ID-3>
  </document>



The task i have is to find the line number for a particular tag. I did some research online and couldn't find a way to get the line numbers.

For e.g., i want to get the line number for tag documentType

I didn't have much luck doing this in C++. i.e., i couldn't find a library that will get me the line numbers. Currently am using MSXML lib.

Can someone guide me whether this is possible to do in C++? Any example code will be really helpful.

What I have tried:

Currently am incrementing an integer in the loop where it traverses through the nodes of the XML. And this integer will hold the line number for a particular tag. This is not a proper logic and will fail if there are line breaks in the XML file.

解决方案

Even an XML file is a text file, so you can search for the position of the tag and than count the CR codes til the position of your tag.

Here is some example code without MFC. Or use the Find function in a similiar way.


Quote:

i couldn't find a library that will get me the line numbers.


The reason is that the line number is meaningless. the lines and indentation exist only to help the reading by humans.
Technically, an XML can be a single line. That is why nobody care about line numbers.

If you really want to use line numbers, I fear you will have to build your own solution.


这篇关于从XML文件中获取行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆