Insertion of XML tag into xml file

Answered

Hi all,
I am trying to insert a xml tag into a xml file using the code below:
PsiFile[] xmlPath = PsiShortNamesCache.getInstance(project).getFilesByName(XMLFilename);
XmlFile xmlFile = (XmlFile) xmlPath[0];
DomManagerImpl domManager = (DomManagerImpl) DomManager.getDomManager(e.getProject());
DomElement root = domManager.getFileElement(xmlFile).getRootElement();
final XmlTag rootTag = root.getXmlTag();
final XmlTag findTags = rootTag.findFirstSubTag("application");
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
findTags.add(findTags.createChildTag("service","findTags.getNameSpace()","",false));
}
});

But the code is not working and shows IncorrectOperationException: cannot add Element. I had tried many ways to resolve this but i can't. Its reading the file correctly but writing in xml is a problem. I am an amateur developer, my project has stuck with this part. Any help will be appreciated.Thanks in advance.

0
3 comments

Do you really mean for "findTags.getNameSpace()" to be a string constant?

Please provide the full stack trace of the exception.

0
Avatar
Permanently deleted user

Hi,

Thanks for the reply. This is my stack trace:

 

[ 51743] ERROR - llij.ide.plugins.PluginManager - Element cannot be added
com.intellij.util.IncorrectOperationException: Element cannot be added
at com.intellij.psi.impl.source.tree.CompositePsiElement.addInnerBefore(CompositePsiElement.java:322)
at com.intellij.psi.impl.source.tree.CompositePsiElement.add(CompositePsiElement.java:151)
at com.intellij.psi.impl.source.xml.XmlTagImpl.setAttribute(XmlTagImpl.java:970)
at com.unfold.droid.transact.plugin.actions.NexvaAction$1.run(NexvaAction.java:111)
at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:1002)
at com.unfold.droid.transact.plugin.actions.NexvaAction.actionPerformed(NexvaAction.java:108)
at com.intellij.openapi.actionSystem.ex.ActionUtil.performActionDumbAware(ActionUtil.java:182)
at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter$1.run(ActionMenuItem.java:311)
at com.intellij.openapi.wm.impl.FocusManagerImpl.runOnOwnContext(FocusManagerImpl.java:958)
at com.intellij.openapi.wm.impl.IdeFocusManagerImpl.runOnOwnContext(IdeFocusManagerImpl.java:124)
at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter.actionPerformed(ActionMenuItem.java:281)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at com.intellij.openapi.actionSystem.impl.ActionMenuItem.fireActionPerformed(ActionMenuItem.java:109)
at com.intellij.ui.plaf.beg.BegMenuItemUI.doClick(BegMenuItemUI.java:513)
at com.intellij.ui.plaf.beg.BegMenuItemUI.access$300(BegMenuItemUI.java:45)
at com.intellij.ui.plaf.beg.BegMenuItemUI$MyMouseInputHandler.mouseReleased(BegMenuItemUI.java:533)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:861)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:645)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:380)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

 

 

0

This stack trace is about setting an attribute and can't be caused by your code above. The cause is unclear: addInternal returned null for some reason in CompositePsiElement.addInnerBefore. Debugging inside addInternal might help understand the reason.

0

Please sign in to leave a comment.