2011-04-01 163 views
0

我正在写一个C++函数来解析.xml文件。 我使用过的系统(“grep”);键入系统调用以使用linux命令。现在我的问题是,我想在给定的标签中选择多行。恩。如何在C++中使用linux命令?

content of .xml file 
==================== 
<state> 
<text> hi this is first li </text> 
<sec> dsfn sdlkf sfd </sec> 
</state> 
=================== 

我想打印所有内容和标签。哪些以及如何使用linux命令。 plz帮助我....感谢我推进。

+0

“我怎么用grep” 超级用户所属。 – 2011-04-01 09:56:55

+0

如何生成子进程并使用此处的输出(C++) – sehe 2011-04-01 09:58:08

+0

搜索“XML正则表达式”以找出为什么不使用'grep'来解析XML文件。 – 2011-04-01 17:41:57

回答

1

高管,execve的或系统调用:演示

#include <stdlib.h> 
#include <unistd.h> 

int main(int argc, const char* args[]) 
{ 
    exitcode = system("xsltprox textonly.xslt input.xml > output"); 
// exitcode = exec("xsltproc", "textonly.xslt", "input.xml"); 
    int exitcode; 
} 

前往fopen输出(或std :: fstream的读者( “输出”)如果你喜欢)。请注意,系统很快就是一个安全漏洞,所以您不应该在关键应用程序(如守护进程)中使用它。 使用exec可以重定向。所以你也许可以从你的程序中打开一个pipe(),fork(),在孩子中将stdout filedescriptor分配给pipe()(参见dup2调用)和exec

父进程然后可以从管道读取输出而不需要临时文件。 所有这些都应该放在一个zillion教程中,我现在不需要时间去google了。


我可以建议使用perl或shell脚本。如果您发布一个实际的例子,我可以告诉你在这两个外壳和C/C++

更新这里是如何用XSLT

textonly.xslt做代码:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" indent="yes"/> 
    <xsl:template match="/"> 
     <xsl:for-each select="//*[text()]"> 
      <xsl:if test="text()"> 
           <xsl:value-of select="text()"/>: 
      </xsl:if> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

输出演示:

[email protected] ~/stacko 
$ xsltproc.exe textonly.xslt input.xml 

: 
      hi this is first li : 
      dsfn sdlkf sfd : 
+0

请注意,使用libxml2/libxslt,您可以轻松地在几行C代码 – sehe 2011-04-01 10:15:17

+0

中轻松完成等同工作,我们可以在Linux上使用C++来完成。 canu告诉ne如何去做。 – balaji 2011-04-01 10:33:58

+0

添加了一些提示和指针 – sehe 2011-04-01 11:01:16

4

用grep解析XML实在不是一个好主意,你应该使用一些C++的xml解析库像这样的: http://xerces.apache.org/xerces-c/index.html

+0

或使用libxml2(使用xml ++ C++绑定) – doron 2011-04-01 10:11:38

+0

我刚刚发布了一个使用xslt的CLI示例 – sehe 2011-04-01 10:14:40

+0

干净利落的解决方案! – 2011-04-01 10:29:56

1

使用C函数popen()pclose()

这里有一个如何使用它们的例子:

#include <string> 
#include <cstdio> 
#include <errno.h> 
#include <fstream> 

int execShellCommand(const std::string command, 
         std::string& commandOutput) 
{ 
    FILE *fp; 
    char charBuf; 
    std::ostringstream stdoutBuffer; 
    int status; 
    std::ostringstream errmsg; 

    // Execute command and save its output to stdout 
    fp = popen(command.c_str(), "r"); 
    if (fp == NULL) { 
    // error handling 
    } else { 
    while ((charBuf = fgetc(fp)) != EOF) { 
     stdoutBuffer << charBuf; 
    } 
    } 

    // Wait for completion of command and return its exit status 
    status = pclose(fp); 
    if (status == -1) { 
    // error handling 
    } else { 
    commandOutput = stdoutBuffer.str(); 
    return WEXITSTATUS(status); // convert exit status 
    } 
} 

int main(){ 
    std::string command = "l | grep lib"; 
    std::string commandOutput; 

    int exitCode = execShellCommand(command, commandOutput); 
} 
+0

非常好。我怎么想这些!谢谢 – sehe 2011-04-01 13:44:22