From: Axel Kielhorn <tex@axelkielhorn.de>
To: "ntg-context@ntg.nl" <ntg-context@ntg.nl>
Subject: Reading XML with lua
Date: Fri, 3 May 2019 17:24:39 +0200 [thread overview]
Message-ID: <6B1CE296-3FCD-4B16-960B-484FE2D25999@axelkielhorn.de> (raw)
[-- 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
___________________________________________________________________________________
next reply other threads:[~2019-05-03 15:24 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-05-03 15:24 Axel Kielhorn [this message]
2019-05-06 9:04 ` Hans Hagen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6B1CE296-3FCD-4B16-960B-484FE2D25999@axelkielhorn.de \
--to=tex@axelkielhorn.de \
--cc=ntg-context@ntg.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).