[Expat-discuss] Text data handler
Thomás Inskip
tinskip at widevine.com
Fri May 21 15:44:57 EDT 2004
Sure thing.
// creates parser, sets handler
XMLConnectionEncoder::XMLConnectionEncoder(EncodedConnection
&connection) :
ConnectionEncoder(connection)
{
mParser = XML_ParserCreate(NULL);
XML_SetUserData(mParser, this);
XML_SetElementHandler(mParser,
XMLConnectionEncoder::Element_Start,
XMLConnectionEncoder::Element_End);
XML_SetCharacterDataHandler(mParser,
XMLConnectionEncoder::Element_Text);
}
void XMLConnectionEncoder::Decode(EncodedConnectionBuffer &source)
{
XML_Parse(mParser, &source[0], source.size(), false);
}
void XMLCALL XMLConnectionEncoder::Element_Start(void *userData,
const XML_Char *name,
const XML_Char
**attributes)
{
XMLConnectionEncoder *encoder = (XMLConnectionEncoder *)userData;
const XML_Char **attrPtr = attributes;
EM_Element *theElement = new EM_Element;
theElement->SetName(name);
while (*attrPtr != NULL) {
theElement->AddAttribute(attrPtr[0], attrPtr[1]);
attrPtr += 2;
}
encoder->mStack.push_back(theElement);
encoder->mText.clear();
}
void XMLCALL XMLConnectionEncoder::Element_End(void *userData,
const XML_Char *name)
{
XMLConnectionEncoder *encoder = (XMLConnectionEncoder *)userData;
if (encoder->mStack.size() > 0) {
EM_Element *theElement = encoder->mStack.back();
encoder->mStack.pop_back();
if (encoder->mText.size() > 0)
theElement->SetValue(encoder->mText);
if (encoder->mStack.size() > 0) {
encoder->mStack.back()->AddElement(theElement);
delete theElement;
} else
encoder->Receive(theElement);
} else {
// Do someething else here
Dprintf("Mismatched calls to
XMLConnectionEndoder::Element_Start and
XMLConnectionEndoder::Element_End");
}
}
void XMLCALL XMLConnectionEncoder::Element_Text(void *userData,
const XML_Char *text,
int len)
{
XMLConnectionEncoder *encoder = (XMLConnectionEncoder *)userData;
encoder->mText.append(text, len);
}
That's all of it, actually. The last three member functions of
XMLConnectionEncoder (Element_*) are static.
An example of the xml being parsed:
3c 45 6e 63 72 79 70 74 41 73 73 65 74 20 53 65 <EncryptAsset Se
73 73 69 6f 6e 49 44 3d 22 30 22 20 53 65 71 75 ssionID="0" Sequ
65 6e 63 65 4e 75 6d 62 65 72 3d 22 31 22 3e 3c enceNumber="1"><
49 6e 70 75 74 46 69 6c 65 3e 2f 68 6f 6d 65 2f InputFile>/home/
74 6f 6d 61 73 2f 6d 70 65 67 2f 63 6e 6e 2e 6d tomas/mpeg/cnn.m
70 67 3c 2f 49 6e 70 75 74 46 69 6c 65 3e 3c 4f pg</InputFile><O
75 74 70 75 74 46 69 6c 65 3e 2f 76 61 72 2f 74 utputFile>/var/t
6d 70 2f 30 30 30 30 30 30 30 30 32 30 2e 6d 70 mp/0000000020.mp
67 3c 2f 4f 75 74 70 75 74 46 69 6c 65 3e 3c 41 g</OutputFile><A
and so on.. but my character data handler never gets called. Afaik it
looks like valid XML.
Thanks.
On May 21, 2004, at 3:23 PM, Karl Waclawek wrote:
>
> ----- Original Message -----
> From: "Thomás Inskip" <tinskip at widevine.com>
> To: <expat-discuss at libexpat.org>
> Sent: Friday, May 21, 2004 3:16 PM
>
>
>> I am new to Expat (been using it for a few hours), and have run into a
>> snag. I am hoping someone can tell me what I am doing wrong.
>>
>> I am trying to parse some pretty simple ascii-encoded XML. I have set
>> my element start and end handlers via XML_SetElementHandler. Those
>> get
>> called just fine. I have also set the character data handler via
>> XML_SetCharacterDataHandler, which is not getting called. A sample
>> xml
>> string which would cause the failure would be :
>> <a><b>some text</b></a>
>> I would have expected my character data handler to be called with
>> "some
>> text" when parsing the xml stream in question, but it is not. Am I
>> using the wrong handler? Is there another way to accomplish this.
>> Any
>> help would be appreciated.
>
> The way you describe it it should work.
> Why don't you post (a short relevant piece of) your code?
>
> Karl
>
>
> _______________________________________________
> Expat-discuss mailing list
> Expat-discuss at libexpat.org
> http://mail.libexpat.org/mailman/listinfo/expat-discuss
More information about the Expat-discuss
mailing list