etree icon indicating copy to clipboard operation
etree copied to clipboard

Easier way to get raw xml?

Open jmarkel44 opened this issue 5 years ago • 1 comments

Is there a simpler way to capture the raw xml data from an element?

For example:


eiEvent := evt.FindElement(".//eiEvent" + eiFilter)
.
.
.
rawXML := etree.NewDocumentWithRoot(eiEvent)
ev.RawXML, _ = rawXML.WriteToString() // this will remove the event from the orig doc 

eiEvent.Text() doesn't seem to return anything. Just trying to store the blob data for that particular element.

Thanks

jmarkel44 avatar May 06 '20 13:05 jmarkel44

The Text() function returns the character data after an XML element's opening tag and before the next XML tag. There are two cases to consider. I suspect your case is the second case.

Case 1. Consider the following XML document:

<doc>
  <eiEvent>description</eiEvent>
</doc>

In this case, Text() will return "description".

Case 2. Now consider this document:

<doc>
  <eiEvent>
    <data>description</data>
  </eiEvent>
</doc>

In this case, Text() returns the whitespace between <eiEvent> and <data>.

If the XML between an element's opening tag (<eiEvent>) and closing tag (</eiEvent>) contains tokens other than just character data, then you'll need to reserialize the element's children the way you've done it.

beevik avatar Jul 18 '20 19:07 beevik