[Solved] xml getElementByID how does it work?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
nickGiard
Posts: 24
Joined: Fri Nov 10, 2017 5:57 pm

[Solved] xml getElementByID how does it work?

Post 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
Last edited by nickGiard on Tue Feb 18, 2025 11:00 am, edited 1 time in total.
LibreOffice 6.3 on Windows 10 64bit
JeJe
Volunteer
Posts: 3064
Joined: Wed Mar 09, 2016 2:40 pm

Re: xml getElementByID how does it work?

Post by JeJe »

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
nickGiard
Posts: 24
Joined: Fri Nov 10, 2017 5:57 pm

Re: xml getElementByID how does it work?

Post 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 :( :( :(
LibreOffice 6.3 on Windows 10 64bit
JeJe
Volunteer
Posts: 3064
Joined: Wed Mar 09, 2016 2:40 pm

Re: xml getElementByID how does it work?

Post 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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Bidouille
Volunteer
Posts: 641
Joined: Mon Nov 19, 2007 10:58 am
Location: France

Re: xml getElementByID how does it work?

Post 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
ms777
Volunteer
Posts: 207
Joined: Mon Oct 08, 2007 1:33 am

Re: xml getElementByID how does it work?

Post 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.
nickGiard
Posts: 24
Joined: Fri Nov 10, 2017 5:57 pm

Re: xml getElementByID how does it work?

Post 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
LibreOffice 6.3 on Windows 10 64bit
ms777
Volunteer
Posts: 207
Joined: Mon Oct 08, 2007 1:33 am

Re: xml getElementByID how does it work?

Post 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
nickGiard
Posts: 24
Joined: Fri Nov 10, 2017 5:57 pm

Re: xml getElementByID how does it work?

Post by nickGiard »

Thank you very much ms777
your help and advises are been very useful.
Good coding
NickGiard
LibreOffice 6.3 on Windows 10 64bit
Locked