drmohundro / SWXMLHash

Simple XML parsing in Swift
MIT License
1.41k stars 205 forks source link

Textual representation of children along with their attributes and text values #168

Closed Tulakshana closed 6 years ago

Tulakshana commented 6 years ago

Following is extracted from the xml I'm trying to serialized. I would like to retrieve the elements, attributes and text inside the element 'abody' in their textual format. What is the correct way to go about this?

<abody>
                    <anid>AN0113544370;vj901mar.16;2016Mar08.13:04;v2.2.500</anid>
                    <title id="AN0113544370-1">SOME TITLE </title>
                    <p>SOME TEXT</p>
                    <p>Some text 
                        <ulink href="some url">some linked text</ulink> some more text
                    <hd id="AN0113544370-2">EVENTS</hd>
                    <p>Some text</p>
                    <p>Some text</p>
                    <hd id="AN0113544370-3">LONDON</hd>
                    <p>Some text</p>
                    <p>Some text</p>
                    <p>Some text</p>
</abody>

I tried doing xml["abody"].element?.description. Then the string contains the 'abody' tag also. I only need its children.

Expected output,

                    <title id="AN0113544370-1">SOME TITLE </title>
                    <p>SOME TEXT</p>
                    <p>Some text 
                        <ulink href="some url">some linked text</ulink> some more text
                    <hd id="AN0113544370-2">EVENTS</hd>
                    <p>Some text</p>
                    <p>Some text</p>
                    <hd id="AN0113544370-3">LONDON</hd>
                    <p>Some text</p>
                    <p>Some text</p>
                    <p>Some text</p>
drmohundro commented 6 years ago

It sounds like what you're asking about is essentially an innerXML type of property that would be the string format. Is that correct? It might make sense to call it innerDescription or childrenDescription just to stay consistent with the existing description property, but I'm open to ideas.

Do you have any thoughts?

Tulakshana commented 6 years ago

@drmohundro, Yes, you got it right. I'm looking at something like an innerXML type of property. For the moment my work around is as follows,

        let body = xml['abody']
        var html = ""
        for child in body.children  {
                html += (child.element?.description ?? "")
        }
drmohundro commented 6 years ago

@Tulakshana I just pushed #188 to add this feature... I'll plan on releasing it over the next few days, but if you'd like to check it out on my branch first to offer feedback, I'd welcome it! Thanks again for the suggestion!

drmohundro commented 6 years ago

FYI, I just published 4.7.0 to include this change.