Problem when using XmlTag.createChildTag

I'm switching some code to the openapi for Idea 5.0 and have a spot of
bother inserting sub tags into an XML file. The code was working fine
in 4.5 (with parent.addChild I think, but that is not available anymore
in 5.0). I was trying .createChildTag, but that does not seem to work...

here is the code, any ideas?

<<<<<< code

ModuleRootManager mgr = ModuleRootManager.getInstance( module );
VirtualFile[] rootFiles = mgr.getContentRoots();

if ( rootFiles != null && rootFiles.length > 0 )
{
PsiManager psiMgr = PsiManager.getInstance( project );
VirtualFile descriptorVFile = rootFiles[0].findChild( "myfile.xml" );

if ( descriptorVFile != null )
{
XmlFile pom = (XmlFile) psiMgr.findFile( descriptorVFile );

XmlDocument doc = pom.getDocument();
XmlTag root = doc.getRootTag();
if ( root != null )
{
XmlTag xmlParent = root.findFirstSubTag( "someTag" );
if ( xmlDepParent != null )
{
CommandProcessor.getInstance().executeCommand( project,
new
CommandPsiWriteTag( xmlParent ),
"Add
child tags", "MyGroup" );
}
model.commit();
}
}
}
>>>>>>

and in the CommandPsiWriteTag thread run function something like


<<<<<< other code

XmlTag newTag = parentTag.createChildTag( "childTag",
parentTag.getNamespace(), "test", false );

>>>>>>

3 comments

What happens when you try? You might want to call
xmlParent.getManager().getElementFactory().createTag(..) and then call add() or
addAfter() or addBefore() on xmlParent, if this doesn't work.

Oliver Fink wrote:

I'm switching some code to the openapi for Idea 5.0 and have a spot of
bother inserting sub tags into an XML file. The code was working fine in
4.5 (with parent.addChild I think, but that is not available anymore in
5.0). I was trying .createChildTag, but that does not seem to work...

here is the code, any ideas?

<<<<<< code

ModuleRootManager mgr = ModuleRootManager.getInstance( module );
VirtualFile[] rootFiles = mgr.getContentRoots();

if ( rootFiles != null && rootFiles.length > 0 )
{
PsiManager psiMgr = PsiManager.getInstance( project );
VirtualFile descriptorVFile = rootFiles[0].findChild( "myfile.xml" );

if ( descriptorVFile != null )
{
XmlFile pom = (XmlFile) psiMgr.findFile( descriptorVFile );

XmlDocument doc = pom.getDocument();
XmlTag root = doc.getRootTag();
if ( root != null )
{
XmlTag xmlParent = root.findFirstSubTag( "someTag" );
if ( xmlDepParent != null )
{
CommandProcessor.getInstance().executeCommand( project,
new
CommandPsiWriteTag( xmlParent ),
"Add
child tags", "MyGroup" );
}
model.commit();
}
}
}

>>>>>>>


and in the CommandPsiWriteTag thread run function something like


<<<<<< other code

XmlTag newTag = parentTag.createChildTag( "childTag",
parentTag.getNamespace(), "test", false );

>>>>>>>

0

Hi,

.createChildTag just creates tag with given qname in context of the
given tag. One should still call 'add' afterwards

Oliver Fink wrote:

I'm switching some code to the openapi for Idea 5.0 and have a spot of
bother inserting sub tags into an XML file. The code was working fine in
4.5 (with parent.addChild I think, but that is not available anymore in
5.0). I was trying .createChildTag, but that does not seem to work...

here is the code, any ideas?

<<<<<< code

ModuleRootManager mgr = ModuleRootManager.getInstance( module );
VirtualFile[] rootFiles = mgr.getContentRoots();

if ( rootFiles != null && rootFiles.length > 0 )
{
PsiManager psiMgr = PsiManager.getInstance( project );
VirtualFile descriptorVFile = rootFiles[0].findChild( "myfile.xml" );

if ( descriptorVFile != null )
{
XmlFile pom = (XmlFile) psiMgr.findFile( descriptorVFile );

XmlDocument doc = pom.getDocument();
XmlTag root = doc.getRootTag();
if ( root != null )
{
XmlTag xmlParent = root.findFirstSubTag( "someTag" );
if ( xmlDepParent != null )
{
CommandProcessor.getInstance().executeCommand( project,
new
CommandPsiWriteTag( xmlParent ),
"Add
child tags", "MyGroup" );
}
model.commit();
}
}
}

>>>>>>>


and in the CommandPsiWriteTag thread run function something like


<<<<<< other code

XmlTag newTag = parentTag.createChildTag( "childTag",
parentTag.getNamespace(), "test", false );

>>>>>>>



--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0

thanks! that was it...

O

On 2005-09-29 20:21:02 +0200, Maxim Mossienko
<Maxim.Mossienko@jetbrains.com> said:

Hi,

.createChildTag just creates tag with given qname in context of the
given tag. One should still call 'add' afterwards

Oliver Fink wrote:

>> I'm switching some code to the openapi for Idea 5.0 and have a spot of
>> bother inserting sub tags into an XML file. The code was working fine
>> in 4.5 (with parent.addChild I think, but that is not available anymore
>> in 5.0). I was trying .createChildTag, but that does not seem to work...
>>
>> here is the code, any ideas?
>>
>> <<<<<< code
>>
>> ModuleRootManager mgr = ModuleRootManager.getInstance( module );
>> VirtualFile[] rootFiles = mgr.getContentRoots();
>>
>> if ( rootFiles != null && rootFiles.length > 0 )
>> {
>> PsiManager psiMgr = PsiManager.getInstance( project );
>> VirtualFile descriptorVFile = rootFiles[0].findChild( "myfile.xml" );
>>
>> if ( descriptorVFile != null )
>> {
>> XmlFile pom = (XmlFile) psiMgr.findFile( descriptorVFile );
>>
>> XmlDocument doc = pom.getDocument();
>> XmlTag root = doc.getRootTag();
>> if ( root != null )
>> {
>> XmlTag xmlParent = root.findFirstSubTag( "someTag" );
>> if ( xmlDepParent != null )
>> {
>> CommandProcessor.getInstance().executeCommand( project,
>> new
>> CommandPsiWriteTag( xmlParent ),
>> "Add
>> child tags", "MyGroup" );
>> }
>> model.commit();
>> }
>> }
>> }
>>
>>>>>>>>
>>
>> and in the CommandPsiWriteTag thread run function something like
>>
>>
>> <<<<<< other code
>>
>> XmlTag newTag = parentTag.createChildTag( "childTag",
>> parentTag.getNamespace(), "test", false );


0

Please sign in to leave a comment.