David (120)
Usenet Archive
- Joined
- Dec 14, 2003
- Messages
- 3
- Reaction score
- 0
Hi,
I'm trying to parse an xml file and am a bit confused. I have created my class XmlParser.
Also I have 3 other questions.
1-How to the GetAttribute to search for the value of a specific pattern ? I want to get the value of the Count tag.
2- My file does not contain the standalone header ? How can I deal with that. 3- In terms of memory handling is it righ to use the ParserFile method or should i use another one ?
Too many questions I know,
thanks fo any help,
david
<?xml version="1.0"?>
<!DOCTYPE eSearchResult PUBLIC "-//NLM//DTD eSearchResult, 11 May 2002//EN" "http://www.ncbi.nlm.nih.gov/entrez/query/DTD/eSearch_020511.dtd"> <eSearchResult> <Count>338</Count> <RetMax>1</RetMax> <RetStart>0</RetStart> <QueryKey>1</QueryKey> <WebEnv>0qq2WLXpVUiIFEtvIrTlducz5uJT8c0vroAyBVMqIoqZjrVInjNh</WebEnv> <IdList> <Id>14645367</Id> </IdList> <TranslationSet> <Translation> <From>snorna%5BAll+Fields%5D</From> <To>(%22rna,+small+nucleolar%22%5BMeSH+Terms%5D+OR+snorna%5BText+Word 5D)</To> </Translation> </TranslationSet> <TranslationStack> <TermSet> <Term>"rna, small nucleolar"[MeSH Terms]</Term> <Field>MeSH Terms</Field> <Count>144</Count> <Explode>Y</Explode> </TermSet> <TermSet> <Term>snorna[Text Word]</Term> <Field>Text Word</Field> <Count>284</Count> <Explode>Y</Explode> </TermSet> <OP>OR</OP> </TranslationStack> </eSearchResult>
#include <iostream>
#include "XmlParser.h"
using namespace std;
testclass.cpp
int main (int argc, char **argv)
{
FILE* xmlFile;
XmlParser parser;
xmlFile = fopen(argv[1], "r");
if (!parser.parseFile(xmlFile)) {
fprintf(stderr,
"%s at line %d\n",
XML_ErrorString(parser.XML_GetErrorCode),
parser.XML_GetCurrentLineNumber);
return 1;
}
char *test = "count";
parser.getAttribute(test);
while (!feof (xmlFile))
XML_ParserFree(parser);
return 0;
}
XmlParser.cpp
void XmlParser::startElement(const XML_Char* name, const XML_Char** atts)
{
cout <<"Attribut:"<<name <<" Depth:"<< mDepth <<endl;
mDepth++;
}
void XmlParser::endElement(const XML_Char*)
{
mDepth--;
}
void XmlParser::charData(const XML_Char *s, int len)
{
const int leadingSpace = skipWhiteSpace(s);
if (len==0 || len==leadingSpace)
return; // called with whitespace between elements
cout <<" Value:";
fwrite(s, len, 1, stdout);
cout<<" Depth:"<< (mDepth-1) <<endl;
}
const XML_Char* XmlParser::getAttribute(const XML_Char *matchingName)
{
cout << "Match:" << matchingName << endl;
}
I'm trying to parse an xml file and am a bit confused. I have created my class XmlParser.
Also I have 3 other questions.
1-How to the GetAttribute to search for the value of a specific pattern ? I want to get the value of the Count tag.
2- My file does not contain the standalone header ? How can I deal with that. 3- In terms of memory handling is it righ to use the ParserFile method or should i use another one ?
Too many questions I know,
thanks fo any help,
david
<?xml version="1.0"?>
<!DOCTYPE eSearchResult PUBLIC "-//NLM//DTD eSearchResult, 11 May 2002//EN" "http://www.ncbi.nlm.nih.gov/entrez/query/DTD/eSearch_020511.dtd"> <eSearchResult> <Count>338</Count> <RetMax>1</RetMax> <RetStart>0</RetStart> <QueryKey>1</QueryKey> <WebEnv>0qq2WLXpVUiIFEtvIrTlducz5uJT8c0vroAyBVMqIoqZjrVInjNh</WebEnv> <IdList> <Id>14645367</Id> </IdList> <TranslationSet> <Translation> <From>snorna%5BAll+Fields%5D</From> <To>(%22rna,+small+nucleolar%22%5BMeSH+Terms%5D+OR+snorna%5BText+Word 5D)</To> </Translation> </TranslationSet> <TranslationStack> <TermSet> <Term>"rna, small nucleolar"[MeSH Terms]</Term> <Field>MeSH Terms</Field> <Count>144</Count> <Explode>Y</Explode> </TermSet> <TermSet> <Term>snorna[Text Word]</Term> <Field>Text Word</Field> <Count>284</Count> <Explode>Y</Explode> </TermSet> <OP>OR</OP> </TranslationStack> </eSearchResult>
#include <iostream>
#include "XmlParser.h"
using namespace std;
testclass.cpp
int main (int argc, char **argv)
{
FILE* xmlFile;
XmlParser parser;
xmlFile = fopen(argv[1], "r");
if (!parser.parseFile(xmlFile)) {
fprintf(stderr,
"%s at line %d\n",
XML_ErrorString(parser.XML_GetErrorCode),
parser.XML_GetCurrentLineNumber);
return 1;
}
char *test = "count";
parser.getAttribute(test);
while (!feof (xmlFile))
XML_ParserFree(parser);
return 0;
}
XmlParser.cpp
void XmlParser::startElement(const XML_Char* name, const XML_Char** atts)
{
cout <<"Attribut:"<<name <<" Depth:"<< mDepth <<endl;
mDepth++;
}
void XmlParser::endElement(const XML_Char*)
{
mDepth--;
}
void XmlParser::charData(const XML_Char *s, int len)
{
const int leadingSpace = skipWhiteSpace(s);
if (len==0 || len==leadingSpace)
return; // called with whitespace between elements
cout <<" Value:";
fwrite(s, len, 1, stdout);
cout<<" Depth:"<< (mDepth-1) <<endl;
}
const XML_Char* XmlParser::getAttribute(const XML_Char *matchingName)
{
cout << "Match:" << matchingName << endl;
}