From mboxrd@z Thu Jan 1 00:00:00 1970 From: John Kitchin Subject: Re: [SOLVED] How to use xml src block as data input? Date: Mon, 30 Apr 2018 21:14:44 -0700 Message-ID: References: <87efk214uq.fsf@gmail.com> <87d0yidpoa.fsf@gmail.com> <87a7tl8imb.fsf@gmail.com> <87muxk3v4a.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000d8e040056b1d352c" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:53971) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fDMgi-0000lQ-DC for emacs-orgmode@gnu.org; Tue, 01 May 2018 00:14:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fDMgg-0007jO-TZ for emacs-orgmode@gnu.org; Tue, 01 May 2018 00:14:48 -0400 Received: from mail-wr0-x232.google.com ([2a00:1450:400c:c0c::232]:41021) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fDMgg-0007j5-FV for emacs-orgmode@gnu.org; Tue, 01 May 2018 00:14:46 -0400 Received: by mail-wr0-x232.google.com with SMTP id g21-v6so9838513wrb.8 for ; Mon, 30 Apr 2018 21:14:46 -0700 (PDT) In-Reply-To: <87muxk3v4a.fsf@gmail.com> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: stardiviner Cc: emacs-orgmode list , =?UTF-8?Q?Simonyi_Andr=C3=A1s?= --000000000000d8e040056b1d352c Content-Type: text/plain; charset="UTF-8" all things in emacs are elisp... You can use this function: #+BEGIN_SRC emacs-lisp (defun get-xml (name) (save-excursion (org-babel-goto-named-src-block name) (org-element-property :value (org-element-context)))) #+END_SRC #+RESULTS: : get-xml like this in a python (or other general lang) block to get the xml as a string that you parse in the language: #+BEGIN_SRC python :var data=(get-xml "xml-example") import xml.etree.ElementTree as ET root = ET.fromstring(data) for child in root: print(child.tag, child.attrib) #+END_SRC #+RESULTS: : to {} : from {} : heading {} : body {} I don't know about noweb. John ----------------------------------- Professor John Kitchin Doherty Hall A207F Department of Chemical Engineering Carnegie Mellon University Pittsburgh, PA 15213 412-268-7803 @johnkitchin http://kitchingroup.cheme.cmu.edu On Mon, Apr 30, 2018 at 8:36 PM, stardiviner wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA256 > > Thanks, John and Simonyi. This is a solution on Elisp. Not general way. > But as a intermedia step, really workable. I chose this method. > > BTW, is it possible to archived with noweb reference <> ? > Like > > #+begin_src python :var data=<> > ... > #+end_src > > - -- > [ stardiviner ] don't need to convince with trends. > Blog: https://stardiviner.github.io/ > IRC(freenode): stardiviner > GPG: F09F650D7D674819892591401B5DF1C95AE89AC3 > > -----BEGIN PGP SIGNATURE----- > > iQEzBAEBCAAdFiEE8J9lDX1nSBmJJZFAG13xyVromsMFAlrn4LYACgkQG13xyVro > msP8ywf/VaaQkpFcV16/2NDRODkC+iNN6TCp1MRJjYL9qkppa/A8vRGPZSxIG5Sd > d2TEGx+mqRS0EANM/mhJwAIlM5U9SpycBfzasvU7R+Cn9CiehZCnJKA0dBaZiu+q > vPBifqEqOzY873XsaRS2Xty6W4NSrh8aL6hEWxiDJSafjaCxI2FvApg2JnPi6HoC > /sy9eSuIYvG8vCRow9y8E/GFcWE5EXoB/d/mdcEmGz80sB39Tjp4PGvLMyglROMa > sR+8QkUTQAtDP4uEAF7biHAGE2PcELpZVLjzWm0GfZkCdJF9tH8KrEXaorB+hyHn > MWxzlZ/gSNcsROF7DHVaZgCMMiiXuw== > =fImh > -----END PGP SIGNATURE----- > --000000000000d8e040056b1d352c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
all things in emacs are elisp...

You ca= n use this function:

#+BEGIN_SRC emacs-lisp
(defun get-xml (name)
=C2=A0 (save-excursion
=C2= =A0 =C2=A0 (org-babel-goto-named-src-block name)
=C2=A0 =C2=A0 (o= rg-element-property :value (org-element-context))))
#+END_SRC

#+RESULTS:
: get-xml

like this in a python (or other general lang) block to get the xml as a = string that you parse in the language:

#+BEGIN_SRC= python :var data=3D(get-xml "xml-example")
import xml.= etree.ElementTree as ET
root =3D ET.fromstring(data)
fo= r child in root:
=C2=A0 =C2=A0 print(child.tag, child.attrib)
#+END_SRC

#+RESULTS:
: to {}
: from {}
: heading {}
: body {}

I don't know about noweb.

John
=
-----------------------------------
Professor John Kitchin=C2=A0
= Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon= University
Pittsburgh, PA 15213
412-268-7803

On Mon, Apr 30, 2018 at 8:36 PM, stardiviner= <numbchild@gmail.com> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Thanks, John and Simonyi. This is a solution on Elisp. Not general w= ay.
But as a intermedia step, really workable. I chose this method.

BTW, is it possible to archived with noweb reference <<xml-data>&g= t; ?
Like

#+begin_src python :var data=3D<<xml-data>>
...
#+end_src

- --
[ stardiviner ] don't need to convince with trends.
=C2=A0 =C2=A0 =C2=A0 =C2=A0Blog: https://stardiviner.github.io/ =C2=A0 =C2=A0 =C2=A0 =C2=A0IRC(freenode): stardiviner
=C2=A0 =C2=A0 =C2=A0 =C2=A0GPG: F09F650D7D674819892591401B5DF1C95AE89A= C3

-----BEGIN PGP SIGNATURE-----

iQEzBAEBCAAdFiEE8J9lDX1nSBmJJZFAG13xyVromsMFAlrn4LYACgkQG13xyVro
msP8ywf/VaaQkpFcV16/2NDRODkC+iNN6TCp1MRJjYL9qkppa/A8vRGPZSxIG5Sd<= br> d2TEGx+mqRS0EANM/mhJwAIlM5U9SpycBfzasvU7R+Cn9CiehZCnJKA0dBaZiu+q<= br> vPBifqEqOzY873XsaRS2Xty6W4NSrh8aL6hEWxiDJSafjaCxI2FvApg2JnPi6HoC<= br> /sy9eSuIYvG8vCRow9y8E/GFcWE5EXoB/d/mdcEmGz80sB39Tjp4PGvLMyglROMa<= br> sR+8QkUTQAtDP4uEAF7biHAGE2PcELpZVLjzWm0GfZkCdJF9tH8KrEXaorB+= hyHn
MWxzlZ/gSNcsROF7DHVaZgCMMiiXuw=3D=3D
=3DfImh
-----END PGP SIGNATURE-----

--000000000000d8e040056b1d352c--