Find the answer to your Linux question:
Results 1 to 3 of 3
Hello, I'm trying to pass a variable to sed that contains XML. From this XML I would like to extract the parts contained between <image_viewer> </image_viewer> ; and <image_thumb_url> </image_thumb_url> ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2013
    Posts
    1

    Making a script to parse simple XML and send it to variables.


    Hello,

    I'm trying to pass a variable to sed that contains XML.

    From this XML I would like to extract the parts contained between <image_viewer> </image_viewer> ; and <image_thumb_url> </image_thumb_url>

    There is an HTML link between each of those tags, which has no spaces; and each tags + link are on a single line.

    It seems I'm doining it completely wrong, I'm very new to bash...

    Code:
    base64 -w0 "$1" > "$1".64.jpg
    
    apiXml=$(curl -v -F "upload=<$1.64.jpg" -F key="$apiKey" -F format=xml "$apiUrl")
    
    imgView=$(echo "$apiXml" | sed 's/<image_viewer>\(.*\)<\/image_viewer>/\1/')
    imgThumb=$(echo "$apiXml" | sed 's/<image_thumb_url>\(.*\)<\/image_thumb_url>/\1/')
    Everything works up to the imgView=... part, anyone has an idea about this ?

    Thanks

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    hi,
    that code works for me.

    1. post the contents of $apiXml

    2. is sed spitting out any errors?

  3. #3
    Just Joined!
    Join Date
    Apr 2005
    Location
    Helsinki, Finland
    Posts
    7
    Hello,
    Funny question, why use sed for querying information from xml document!?
    Why not use of other tools!?

    so you just need a value from some xml file..right?! Why not something like..

    Code:
    #! /bin/bash
    
    apiXML="foo.xml"
    
     imgView=$(/usr/bin/xmllint --xpath "root/picture/imageView/text()" ${apiXML})
    imgThumb=$(/usr/bin/xmllint --xpath "root/pics/image_thumb_url/text()" ${apiXML} )
    
    echo "$imgView"
    echo "$imgThumb"
    Code:
    <root>
      <picture>
        <imageView>imageView_from_picture_tag</imageView>
      </picture>
      <pics>
        <image_thumb_url>image_thumb_url_from_pics_tag</image_thumb_url>
      </pics>
    </root>
    in case the path of the xml might change then you could simply do
    Code:
    xmllint --xpath "//imageView/text()"  ${apiXML}
    Hope this helps..

    Kind Regards,
    Noel Alex Makumuli

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •