* Reading XML with lua
@ 2019-05-03 15:24 Axel Kielhorn
2019-05-06 9:04 ` Hans Hagen
0 siblings, 1 reply; 2+ messages in thread
From: Axel Kielhorn @ 2019-05-03 15:24 UTC (permalink / raw)
To: ntg-context
[-- Attachment #1: Type: text/plain, Size: 591 bytes --]
Hi,
I’ m finally at a point where I can read an XML file look at it and write it back.
Right now I have two problems:
1. There are many empty entries in the table.
This makes processing the file a little tedious since I have to filter the real entries.
2. Some entries contain leading and trailing whitespace and linkebreaks.
These are the result of formatting the XML file.
Shouldn't
strip_cm_and_dt
take care of them?
Or do I use that incorrectly?
I have included my lua file and an example xml.
You can run it with
mtxrun —script p.lua
Greetings Axel
[-- Attachment #2: doclist.xml --]
[-- Type: application/xml, Size: 2416 bytes --]
[-- Attachment #3: p.lua --]
[-- Type: application/octet-stream, Size: 958 bytes --]
local application = logs.application {
name = "Prozess",
banner = "Prozess parser",
}
local report = application.report
local settings = {}
settings.strip_cm_and_dt=true
-- Reading the file
doc = xml.load("doclist.xml", settings)
report("Datei %s gelesen", doc.settings.currentresource)
-- inspect(doc)
-- inspect(doc.dt)
for i,v in ipairs(doc.dt) do
if v.tg == "doclist" then
doc2 = v
break
end
end
-- Just the doclist part
-- inspect(doc2)
for i,v in ipairs(doc2.dt) do
if v.tg=="psdoc" then
print ("PSDOC: ",i)
-- inspect (v)
docan = nil
for j,k in ipairs (v.dt) do
if k.tg then
print (j, k.tg, table.unpack(k.dt))
if k.tg=="docan" then docan=table.unpack(k.dt) end
-- inspect (k.dt)
end
end
if docan then print ("DOCAN: ", docan) end
end
-- inspect (v)
end
-- Writing it back
xml.save(doc,"outfile.xml")
-- So far no logging
--[[
local f = io.open("p.log", "w")
f:write()
f:close()
--]]
[-- Attachment #4: Type: text/plain, Size: 493 bytes --]
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Reading XML with lua
2019-05-03 15:24 Reading XML with lua Axel Kielhorn
@ 2019-05-06 9:04 ` Hans Hagen
0 siblings, 0 replies; 2+ messages in thread
From: Hans Hagen @ 2019-05-06 9:04 UTC (permalink / raw)
To: mailing list for ConTeXt users, Axel Kielhorn
On 5/3/2019 5:24 PM, Axel Kielhorn wrote:
> Hi,
>
> I’ m finally at a point where I can read an XML file look at it and write it back.
>
> Right now I have two problems:
>
> 1. There are many empty entries in the table.
> This makes processing the file a little tedious since I have to filter the real entries.
>
> 2. Some entries contain leading and trailing whitespace and linkebreaks.
> These are the result of formatting the XML file.
> Shouldn't
> strip_cm_and_dt
> take care of them?
no, as comments and doctype definitions are not the same as spaces
> Or do I use that incorrectly?
the stored xml is largely roundtrip
> I have included my lua file and an example xml.
> You can run it with
> mtxrun —script p.lua
>
> Greetings Axel
>
>
>
>
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
>
> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage : http://www.pragma-ade.nl / http://context.aanhet.net
> archive : https://bitbucket.org/phg/context-mirror/commits/
> wiki : http://contextgarden.net
> ___________________________________________________________________________________
>
--
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-05-06 9:04 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-03 15:24 Reading XML with lua Axel Kielhorn
2019-05-06 9:04 ` Hans Hagen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).