cancel
Showing results for 
Search instead for 
Did you mean: 

updating .xml files through JAVA?

Former Member
0 Kudos

Hi all,

I am Krishna..

I need to basically parse the content of the XML form and do a text replace. The Promotions that are created by the users will also reference images. These references are stored in the XMLforms (which are basically XML files). When the Promotions are being 'submitted' the file path within the XMLForms should be updated with the target location.

<b>Example:</b>

Source Location where Promotion was created:

/myForms/filesystem/Partner Promotion/user1_folder/

Target Location where the Promotion is to be copied:

/myForms/filesystem/Submitted Promotions/user1_folder/

The Promotion XMLForm should be parsed for occurrences of “/myForms/filesystem/Partner Promotion/user1_folder/” and replaced

with “/myForms/filesystem/Submitted Promotions/user1_folder/”

Problem is that, i am unable to build the XML file:

try {
	String rpathString = "/documents/Test/promotions/Contacts.xml";
	
	com.sapportals.portal.security.usermanagement.IUser user =
		WPUMFactory.getUserFactory().getEP5User(request.getUser());
	IResourceContext ctxt = new ResourceContext(user);
	RID rid = RID.getRID(rpathString);
	IResource resource =
		ResourceFactory.getInstance().getResource(rid, ctxt);
	
	//read the xml file
	   SAXBuilder builder = new SAXBuilder();
	  <b>Document doc = builder.build(new File(resource.getName()));</b> <i>//getting exception at this line</i>
	   Element root = doc.getRootElement();
	   List list = root.getChildren("contact");
	   Iterator iter = list.iterator();
	   while(iter.hasNext()) {
		Element e = (Element)iter.next();
		String name = e.getChild("name").getText();
		response.write("<br>name:"+name);
		String address = e.getChild("address").getText();
		response.write(":::address:"+address);
	}
} catch (Exception e) {
   response.write(e.getLocalizedMessage());
   e.printStackTrace();
}

I have also tried with

new File("http://<server>:50000/irj/go/km/docs/documents/Test/promotions/Contacts.xml");

and

new File(resource.getAccessURI());

.. but i don't get it.

<b>PS:</b> i have added <b>jdom.jar</b> file in the build path

Plz help . .. Thanking you in advance!

Regards,

Krish.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

It's going to CATCH block ..and saying:

<b>E:documentsTestpromotionsContacts.xml (The system cannot find the path specified)</b>

Plz help...

Regards,

Krish.

Former Member
0 Kudos

Hi Krish,

The <i>File</i> class doen't apply here.

xmlforms are resources, represented by the class <i>IResource</i>.

I am working with DOM, rather the SAX, but I guess same could work here.

The idea is to get the <i>InputStream</i> of the resource, and use it in the builder:

myResource.getUnfilteredContent().getInputStream().

The writing should then be done by IResource's updateContent.

Usefull javadoc:

http://www.ajvic.es/irj/portalapps/javadocs/km/com/sapportals/wcm/repository/iresource.html

Hope that helps,

Yoav.

EDIT:

Corrected 'setContent' to 'updateContent'

Former Member
0 Kudos

Hi Yoav, All,

Thanks..

Now, I have the output as a String.

String OutputString = OriginalString.replaceAll("/myForms/~filesystem~/Partner Promotion/user1_folder/","/myForms/~filesystem~/Submitted Promotions/user1_folder/");

How to convert this OutputString into IContent ?? so that i can use it in

resource.updateContent(??????????????);

for updating the resource (XML file) ..

Plz help..

Regards,

Krish..

Answers (1)

Answers (1)

Former Member
0 Kudos

Well, a resource in KM is not a Java file.

Have a look at

IResource.getUnfilteredContent()

.

Best regards, Julian