Newbie needs helping inserting an element in an XML file

34 views
Skip to first unread message

CatDude

unread,
Sep 21, 2011, 4:10:03 PM9/21/11
to akara
I new to Amara but reasonably proficient with Python.

I need to add entries to an XML file used by Wowza Media Server. The
structure looks like:
<Root>
<StartupStreams>
<StartupStream
<Application>App name 1</Application>
<MediaCasterType>liverepeater</MediaCasterType>
<StreamName>MyLiveStream</StreamName>
</StartupStream>
...
... more StartupStream entries
...
</StartupStreams>
</Root>

I am able to read the file in and parse it just fine. What I need to
be able to do is insert a new StartupStream entry. I've looked at
doc.xml_append(doc.xml_element_factory) but I must admit that the
Python docs that come up in the Python shell are not much help in
figuring out what I'm doing.

Could anyone provide some suggestions as to where I can find the info
I need?

Luis Miguel Morillas

unread,
Sep 21, 2011, 4:50:39 PM9/21/11
to ak...@googlegroups.com
2011/9/21 CatDude <cat...@gmail.com>:

> I new to Amara but reasonably proficient with Python.
>
> I need to add entries to an XML file used by Wowza Media Server. The
> structure looks like:
> <Root>
>    <StartupStreams>
>        <StartupStream
>            <Application>App name 1</Application>
>            <MediaCasterType>liverepeater</MediaCasterType>
>            <StreamName>MyLiveStream</StreamName>
>        </StartupStream>
> ...
> ...    more StartupStream entries
> ...
>    </StartupStreams>
> </Root>
>
> I am able to read the file in and parse it just fine. What I need to
> be able to do is insert a new StartupStream entry. I've looked at
> doc.xml_append(doc.xml_element_factory) but I must admit that the
> Python docs that come up in the Python shell are not much help in
> figuring out what I'm doing.
>

We're documenting Amara/Akara projects at http://wiki.xml3k.org. You
can visit Amara's tutorial: http://wiki.xml3k.org/Amara/Tutorial There
you have go xml_append examples (if you're using amara.parse function)
You can use too bindery methods that are much more pythonic because
you can use xml elements as python objects.

-- lm

> Could anyone provide some suggestions as to where I can find the info
> I need?
>

> --
> You received this message because you are subscribed to the Google Groups "akara" group.
> To post to this group, send email to ak...@googlegroups.com.
> To unsubscribe from this group, send email to akara+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/akara?hl=en.
>
>

D Mahoney

unread,
Sep 21, 2011, 5:10:18 PM9/21/11
to ak...@googlegroups.com
I've read through the tutorial, but it still leaves me unclear.

For example, I'm doing this:
from amara import bindery

doc = bindery.parse(''.join(open('./StartupStreams.xml').readlines()))
top = doc.Root
streamContainer = doc.Root.StartupStreams

I've now got my XML file in a nice, easy to navigate form. I can list
the current contents of the tree with:
stream = streamContainer.StartupStream
for strm in stream:
print strm.Application, strm.StreamName

and this shows me that everything looks good.

Now, I can define the XML I want to insert easily enough:

XML = """<StartupStream>
<Application>myAppName</Application>
<MediaStreamType>liverepeater</MediaStreamerType>
<StreamName>MyLiveStream</StreamName>
</StartupStream>"""

and I can convert into a node that Amara understands:
node = bindery.parse(XML)

Now is where I'm lost. I've tried:
streamContainer.xml_append(streamContainer.xml_element_factory(None, node))
and I've tried:
streamContainer.xml_append(streamContainer.xml_element_factory(None, XML)
but no matter what I try when I iterate through the tree again I never
see the element I've tried to insert. I must be missing something very
simple, which I'm hoping someone can point out to me.

Uche Ogbuji

unread,
Sep 23, 2011, 12:53:43 PM9/23/11
to ak...@googlegroups.com
Hello.
Sorry the docs weren't sufficient. I'll see what I can improve quickly for this case. The following works for me:

-- % --

XML = """\
<Root>
   <StartupStreams>
       <StartupStream>
           <Application>App name 1</Application>
           <MediaCasterType>liverepeater</MediaCasterType>
           <StreamName>MyLiveStream</StreamName>
       </StartupStream>
...
...    more StartupStream entries
...
   </StartupStreams>
</Root>
"""

from amara import bindery

doc = bindery.parse(XML)
top = doc.Root
streamContainer = doc.Root.StartupStreams

MORE_XML = """<StartupStream>
<Application>myAppName</Application>
<MediaStreamType>liverepeater</MediaStreamType>
<StreamName>MyLiveStream</StreamName>
</StartupStream>"""

streamContainer.xml_append_fragment(MORE_XML)

doc.xml_write()

-- % --

HTH

--
Uche Ogbuji                       http://uche.ogbuji.net
Weblog: http://copia.ogbuji.net
Poetry ed @TNB: http://www.thenervousbreakdown.com/author/uogbuji/
Founding Partner, Zepheira        http://zepheira.com
Linked-in: http://www.linkedin.com/in/ucheogbuji
Articles: http://uche.ogbuji.net/tech/publications/
Friendfeed: http://friendfeed.com/uche
Twitter: http://twitter.com/uogbuji
http://www.google.com/profiles/uche.ogbuji

D Mahoney

unread,
Sep 23, 2011, 2:34:40 PM9/23/11
to ak...@googlegroups.com
Very cool! Thanks a bunch.

On 09/23/2011 11:53 AM, Uche Ogbuji wrote:
> Hello.

Luis Miguel Morillas

unread,
Sep 25, 2011, 4:00:50 PM9/25/11
to ak...@googlegroups.com
2011/9/23 D Mahoney <cat...@gmail.com>:


Or if the fragment was a parsed node:

MORE_XML = """<StartupStreams>
<StartupStream>
<Application>myAppName2</Application>
<MediaStreamType>liverepeater2</MediaStreamType>
<StreamName>MyLiveStream2</StreamName>
</StartupStream>
<StartupStream>
<Application>myAppName3</Application>
<MediaStreamType>liverepeater3</MediaStreamType>
<StreamName>MyLiveStream3</StreamName>
</StartupStream>
</StartupStreams>"""

fragment_node = bindery.parse(MORE_XML)

# I'm going to add only the second StartupStream
streamContainer.xml_append(fragment_node.StartupStreams.StartupStream[1])

doc.xml_write()


>>>
<?xml version="1.0" encoding="UTF-8"?>


<Root>
<StartupStreams>
<StartupStream>
<Application>App name 1</Application>
<MediaCasterType>liverepeater</MediaCasterType>
<StreamName>MyLiveStream</StreamName>
</StartupStream>
...
... more StartupStream entries
...

<StartupStream>
<Application>myAppName3</Application>
<MediaStreamType>liverepeater3</MediaStreamType>
<StreamName>MyLiveStream3</StreamName>
</StartupStream></StartupStreams>
</Root>

-- lm

Reply all
Reply to author
Forward
0 new messages