0

i am writing a c++ function that will parse .xml file. i Used system("grep"); type system call to use linux command. now my problem is that i want to select more than one line in given tag. ex.

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

i want to print all content between and tag. which and how i can use linux commands. plz help me....thanks i advance.

3
  • "How do I use grep" belongs on Super User. Commented Apr 1, 2011 at 9:56
  • how do I spawn a subprocess and use the output belongs here (C++) Commented Apr 1, 2011 at 9:58
  • Search SO for "XML regular expression" to find out why not to use grep for parsing XML files. Commented Apr 1, 2011 at 17:41

3 Answers 3

4

Parsing XML using grep is really not a good idea, you should use some c++ xml parsing library like this one: http://xerces.apache.org/xerces-c/index.html

Sign up to request clarification or add additional context in comments.

2 Comments

or use libxml2 (with xml++ C++ bindings)
i just posted a CLI example using xslt
1

exec, execve or system calls: demo

#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;
}

Proceed to fopen output (or std::fstream reader("output") if you fancy). Note that system is quickly a security hole, so you shouldn't use it in critical apps (like daemons). Using exec you could redirect. So you could perhaps open a pipe() from your program, fork(), in the child assign the stdout filedescriptor to the pipe() (see dup2 call) and exec in the child.

The parent process can then read the output from the pipe without need for a temp file. All of this should be on a zillion tutorials, which I don't have to the time to google for you right now.


May I suggest using perl or shell script. If you post an actual example I could show you the code in both shell and c/c++

Update here is how to do it with 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>

output demo:

o399hee@OWW00837 ~/stacko
$ xsltproc.exe textonly.xslt input.xml

 :
             hi this is first li :
             dsfn  sdlkf sfd :

2 Comments

Note that using libxml2/libxslt you can do the equivalent easily in a few lines of C code
can we do it in C++ on linux. canu tell ne how to do it.
1

Use the C functions popen() and pclose()

Here's an example of how to use them:

#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);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.