Page 1 of 1
[Solved] xml getElementByID how does it work?
Posted: Sun Feb 16, 2025 11:17 am
by nickGiard
Xml module is very interesting, if i want write something like tree, like a manual with topics, it is the solution.
Take a simple file xml with ID :
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<Root ID="0">
<Elem ID="A" Txt="Capitolo A"></Elem>
<Elem ID="B" Txt="Capitolo B"></Elem>
</Root>
I load a DOM
Code: Select all
Dim oDocB : oDocB = CreateUnoService("com.sun.star.xml.dom.DocumentBuilder")
Dim xDoc : xDoc = oDocB.parseURI(sXmlPath) ' percorso del file xml
Now I'd like to get the element whit ID="B" and see that interface xDocument has getElementById(string elementId) method, so :
Code: Select all
Dim MyElem : MyElem = xDoc.getElementById("B")
This does not work, any idea ???
Thanks in advance
Re: xml getElementByID how does it work?
Posted: Sun Feb 16, 2025 1:22 pm
by JeJe
Re: xml getElementByID how does it work?
Posted: Sun Feb 16, 2025 7:16 pm
by nickGiard
Thank you JeJe
I have seen the link, but it is much difficult for me.
I hope an easy answer for an easy question, or not? But nobody seems to give

Re: xml getElementByID how does it work?
Posted: Sun Feb 16, 2025 7:51 pm
by JeJe
There's hardly anything to the Basic code as most of it is (necessary) empty subs and comments, and you don't need to understand it to run it - except where you change the URL to your file location.
Edit: whether that will read your file or not, I don't know, but I've seen it work with other xml files.
Re: xml getElementByID how does it work?
Posted: Sun Feb 16, 2025 8:16 pm
by Bidouille
nickGiard wrote: ↑Sun Feb 16, 2025 11:17 am
This does not work, any idea ???
Did you try a search in this forum?
viewtopic.php?t=110832&hilit=documentbuilder
Re: xml getElementByID how does it work?
Posted: Sun Feb 16, 2025 9:56 pm
by ms777
Hi,
replace your xml by:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Root [
<!ATTLIST Elem id ID #IMPLIED >
<!ATTLIST Root id ID #IMPLIED >
]>
<Root id="0">
<Elem id="A" Txt="Capitolo A"></Elem>
<Elem id="B" Txt="Capitolo B"></Elem>
</Root>
You need to include the ATTLIST lines for each TagName which is supposed to have an id.
Re: xml getElementByID how does it work?
Posted: Mon Feb 17, 2025 7:13 pm
by nickGiard
thank you very much ms777
you open my eyes on DTD, I never used it !!!
It work fine, I have debug it and it is Ok ....
... but when in basic debug window examine xDoc.FirstChild LibreOffice goes to crash :
https://crashreport.libreoffice.org/sta ... 5fb511226e
All is Ok if examine xDoc.DocumentElement.FirstChild and so on.
In Notepad++ show : DTD is not allowed, and in Plugin XMLTools Options Property, I read Prohibit DTD for security.
Do you noticed this?
In the past I resolved my issue creating a dictionary EnumerableMap and while parsing xElements put(id, xElement)
By
Re: xml getElementByID how does it work?
Posted: Mon Feb 17, 2025 10:05 pm
by ms777
Hi,
I can reproduce your observations ...
Probably this is related to
https://stackoverflow.com/questions/138 ... -exception. Please keep in mind that the LibreOffice code is probably 30 years old.
Depending on the complexity of your target I would recommend to stay in Basic and do not use DTD / getElementById or to switch to python and use xml.etree.ElementTree, which has basic XPath support
Or you restrict your access to DocumentElement. This seems to work fine ...
Good luck,
ms777
Re: xml getElementByID how does it work?
Posted: Tue Feb 18, 2025 10:58 am
by nickGiard
Thank you very much ms777
your help and advises are been very useful.
Good coding
NickGiard