[Solved] Change an empty node value with DocumentBuilder

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

[Solved] Change an empty node value with DocumentBuilder

Post by Mr.Dandy »

Come back with a new issue :(

My XML is

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<item>
 <state>true</state>
 <name/>
 <location>A</location>
 <version>13</version>
 <features>None</features>
</item>
Change an existing value works but not if this is empty

Code: Select all

Sub Main
	sURL = ConvertToUrl("c:\Temp\test.xml")
	XmlChangeValue(sURL, "version", 14) 'works
	XmlChangeValue(sURL, "name", "Foo") 'failed
	
End Sub

Function XmlChangeValue(sURL, sNode, sValue)
	oDocBuilder = createUnoService("com.sun.star.xml.dom.DocumentBuilder")
	oDom = oDocBuilder.parseURI(sURL)
	oDom.normalize()
	
	oXmlNode = oDom.getElementsByTagName(sNode)
	nNode = oXmlNode.getLength()
	for i = 0 To nNode - 1
		oChildNode = oXmlNode.item(i).getChildNodes()
		if oChildNode.Length > 0 then
			oElem = oChildNode.item(0)
			oElem.normalize()
			oElem.deleteData(0, Len(oElem.getData() )
			oElem.setData(sValue)
		else
			oElem = oXmlNode.item(0)
			oElem.normalize()
			'mri oElem
			oElem.setNodeValue(sValue)
		endif
	next i

	oSFA = createUNOService ("com.sun.star.ucb.SimpleFileAccess") 
	'oOutStream = oSFA.openFileWrite(sURL) 
	oOutStream = oSFA.openFileWrite("c:\Temp\test2.xml") 'keep origin
	oDom.setOutputStream(oOutStream) 
	oDom.start
	oOutStream.closeOutput()

End function
setNodeValue occurs an exception
Did you have an advice or (better) a fix?
Thanks
Last edited by Mr.Dandy on Sat Nov 18, 2023 11:20 am, edited 1 time in total.
OpenOffice 4.1.12 - Windows 10
JeJe
Volunteer
Posts: 2814
Joined: Wed Mar 09, 2016 2:40 pm

Re: Change an empty node value with DocumentBuilder

Post by JeJe »

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: Change an empty node value with DocumentBuilder

Post by Mr.Dandy »

I don't understand what I do with this page
The c.s.s.xml.dom.DOMException returns no message:
capture.png
capture.png (116.06 KiB) Viewed 2328 times
OpenOffice 4.1.12 - Windows 10
JeJe
Volunteer
Posts: 2814
Joined: Wed Mar 09, 2016 2:40 pm

Re: Change an empty node value with DocumentBuilder

Post by JeJe »

Using MRI I see its an element node - they don't have a node value, hence an exception. See the table on this page pointed to by the setNodeValue documentation:

https://www.openoffice.org/api/docs/com ... tNodeValue
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: Change an empty node value with DocumentBuilder

Post by Mr.Dandy »

However I see a NodeValue property
capture.gif
capture.gif (96.11 KiB) Viewed 2274 times
OpenOffice 4.1.12 - Windows 10
JeJe
Volunteer
Posts: 2814
Joined: Wed Mar 09, 2016 2:40 pm

Re: Change an empty node value with DocumentBuilder

Post by JeJe »

Yes, and the documentation says its null for an element node and that setNodeValue will return the error you get. It is all explained, and as expected.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Bidouille
Volunteer
Posts: 579
Joined: Mon Nov 19, 2007 10:58 am
Location: France

Re: Change an empty node value with DocumentBuilder

Post by Bidouille »

Rather than beating about the bush

Instead of

Code: Select all

oElem.setNodeValue(sValue)
Replace by

Code: Select all

oElem.appendChild(oDom.createTextNode(sValue))
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: Change an empty node value with DocumentBuilder

Post by Mr.Dandy »

Bidouille wrote: Thu Nov 16, 2023 12:05 pm Rather than beating about the bush
Indeed

Thanks for the code: solved! :)
OpenOffice 4.1.12 - Windows 10
Post Reply