GeeCON Prague 2014*
The moose likes Ant, Maven and Other Build Tools and the fly likes Error while running Ant script for appending an XML tag using XMLTask Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Error while running Ant script for appending an XML tag using XMLTask" Watch "Error while running Ant script for appending an XML tag using XMLTask" New topic
Author

Error while running Ant script for appending an XML tag using XMLTask

raghu rama
Greenhorn

Joined: Dec 20, 2012
Posts: 12
Hi all,

I am trying to append an xml tag to my existing xml file using XMLTask (external task) in Ant Script.Following is my code for that






When i try to build the above Ant script, I get the following error taskdef class com.oopsconsultancy.xmltask.ant.XmlTask cannot be found
using the classloader AntClassLoader[]
I dont understand why i am getting this error,Can anybody help me in resolving this issue? Thank you in advance.




Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5835
    
    7

You need to add place the JAR file containing XmlTask into the classpath. Do this by either adding a classpath or classpathref attribute to the <taskdef> (recommended), or by placing the JAR file in the ant_home/lib directory.


JBoss In Action
raghu rama
Greenhorn

Joined: Dec 20, 2012
Posts: 12
Thanks a lot for the reply Peter,

I did as you said,following is the code change that i have made




The class not found error has been resolved,but iam getting the following error when i try to execute the script,



It looks like you've got a network error. The probable cause
is that you're trying to resolve a DTD on the internet although
you don't know it! Check your XML for DTDs external to your network
and read the Ant documentation for <xmlcatalog>. XMLTask will support
usage of <xmlcatalog>. See the following:
http://ant.apache.org/manual/CoreTypes/xmlcatalog.html
http://www.oopsconsultancy.com/software/xmltask
If this isn't the problem, then please report this error to the support


Any suggestions?
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5835
    
    7

Do what the error message says - check the XML you are processing to see if it references any DTDs or XSDs.

By the way, are you dong this from home or from work? If from work, your IT department might be blocking the network request - you might want to ask them about it (though good luck in getting them to change anything!). In that case you might have to remove the DTDs from the XML before processing it and then add it back in afterwards (yes, this is an ugly solution).
raghu rama
Greenhorn

Joined: Dec 20, 2012
Posts: 12
Hi Peter,

Thanks for the solution and sorry for the delay in response .As you had guessed i executed the script when i was in my office and yes after removing the DTD i was able to run the script.

I have one issue regarding the xml tag insertion ,Following is the xml in the source file


input.xml



I am copying the source file contents to a new xml file and i am trying to add a tag after the session factory tag

Following is the ant script for that


Though the build is succesful and the contents of the source file are present in the destination file ,the tag is not getting inserted


Can you please suggest where i had gone wrong? thank you in advance
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5835
    
    7

I suspect that the path value for the <insert> is incorrect, it probably requires an XPath value. I'm not an XPath expert, but I would try path="/hibernate-configuration/session-factory"
raghu rama
Greenhorn

Joined: Dec 20, 2012
Posts: 12
Hats off you to Peter ..I did as you said and have got the correct output, following are the code changes that i have made
Once i again thanks for your time and patience


input.xml



output.xml




Adding to the above points i have a small query ....u had earlier mentioned that i need to manually remove DTD from XML files...is there any task in Ant or XMLTask to comment/remove the DTD of an XML file?
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5835
    
    7

Realize that when an XML file is opened by an XML parser that the DTD is evaluated as part of opening the file. Thus you cannot use any XML-specific task to do this job. However, you could first copy the XML file as text and use a filter to remove the DTD. Check out the lineContainsRegex filterchain: http://ant.apache.org/manual/Types/filterchain.html#linecontainsregexp
raghu rama
Greenhorn

Joined: Dec 20, 2012
Posts: 12
Hi Peter,
thanks for the reply , I did as you have advised.
I copied the contents of the xml to a text file and tried using lineContainsRegex filterchain.However i couldnot remove the DTD form the text file, I then tried
using the following task

<replace file="lib/internotepad.txt">
<replacetoken><![CDATA[<!DOCTYPE]]></replacetoken>
<replacevalue><![CDATA[<!--]]></replacevalue>
</replace>

<replace file="lib/internotepad.txt">
<replacetoken><![CDATA[.dtd">]]></replacetoken>
<replacevalue><![CDATA[.dtd"-->]]></replacevalue>
</replace>
which commented the DTDs and solved my problem.
Please provide your valuable feedback on this do correct me if have done any mistake.
Thanks a lot for your time and patience
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5835
    
    7

I usually don't like updating files in-place; whenever you do that you have to remember not to commit the file to the source code repository, which can become a nightmare it you accidentally forget and let it happen anyway (I constantly curse one of our third-party apps that insists on doing this very same thing, and thus guarantees that the builds fail 50% of the time). If a <replace> works, you should be able to do a <copy> with a <filterchain). (If you cant't figure that out, at least copy the file first and then do the replace on the copy.)

In general, NEVER change any source files. Instead, always copy to another location to manipulate them. This is one reason why I recommend that everyone learn Maven, if nothing but to emulate how Maven handles files and directories. If you look at the source code for my book, you'll see that I use Ant to do builds but I followed the Maven directory structure and Maven's best practice for never modifying source files.>
 
GeeCON Prague 2014
 
subject: Error while running Ant script for appending an XML tag using XMLTask