on 05-02-2008 6:47 AM
Hi all,
I want to write to an existing xml file. Like I want to insert new element from webdynpro. Please let me know as how to do this.
regards,
Satish
hi,
say u have xml with following structure
<xml>
<Name>xyz</Name>
</xml>
to append age node, parse the xml string or file and try the following code
HERE DOM is used
Document xmlDoc = wdThis.ParseFile(xmlString);
Node root=xmlDoc.getDocumentElement();
Node AgeNode = xmlDoc.createElement("Age");
Node AgeTextNode = xmlDoc.createTextNode("25");
AgeNode.appendChild(AgeTextNode);
root.appendChild(AgeNode);
following code to parse the xml string
public org.w3c.dom.Document mParseFile( java.lang.String XmlString )
{
//@@begin mParseFile()
//pearse the xml string passed
IWDMessageManager mesg = wdComponentAPI.getMessageManager();
String l_method = "mParseFile";
//parsing xml string to xml document
ByteArrayInputStream l_xmlDataInputStream = new ByteArrayInputStream(p_XmlString.getBytes());
Document l_doc = null;
InputSource in = new InputSource((InputStream) l_xmlDataInputStream );
DocumentBuilderFactory l_dbf = DocumentBuilderFactory.newInstance();
l_dbf.setValidating(true);
l_dbf.setNamespaceAware(true);
DocumentBuilder l_db;
try {
l_db = l_dbf.newDocumentBuilder();
l_doc = l_db.parse(in);
}
catch (ParserConfigurationException e) {
} catch (SAXException e) {
} catch (IOException e) {
}
//returns xml document
return l_doc;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
in my project i have created/edtited XML file and stored in the KM.for that i have used XML builder, XML handler and XML parser. You also need to do the same.
In that
XMLBuilder:- this class used to create XML file structure according to your need.
XMLHandler:- this class XMLHandler extends DefaultHandler.
XMLParser:-this class used to parse the XML, for example one of the function of this class which i have created in my project is:-
public Event getEvent(String newsXML,XMLHandler xmlHandler,IWDMessageManager mgr)throws SAXException, ParserConfigurationException, IOException {
//create the parser, if not already done
if (parser == null) {
this.createParser();
}
//set the parser handler to extract the
reader.setErrorHandler(xmlHandler);
reader.setContentHandler(xmlHandler);
InputSource source = new InputSource(new ByteArrayInputStream(newsXML.getBytes()));
reader.parse(source);
//return the results of the parse
return xmlHandler.getEvent(mgr);
}
Hope that may help you.
Regards,
Deepak
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Satish,
you need to add value for a tag if i m not wrong. Will you plz give me the detail like what value you are going to enter. is this value is going to be enter by user in any WebDynpro UI element..?
plz bit clear.
coz in my case i have get the value entered by the user and on the click of the save button i have to create the XML and store in the KM.
Regards,
Deepak
Hello satish,
I think you may have to follow the last solution this time too.
XML Since XML has s specific structure; on adding/appending a new element in will be difficult. but you can process the entire source (that could be a table-content) and again write to a file of format XML.
I think; this is the best solution. And what you need is a single method call( described in the tableUtility.java).
regards
Vinod V
hi,
Please go through the below mentioned blog for parsing xml files:
/people/community.user/blog/2006/10/18/how-to-parse-xml-file-uploaded-from-client
Following doc will also help inparsing
http://java.sun.com/developer/Books/xmljava/ch03.pdf
regards,
amit bagati
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
101 | |
13 | |
13 | |
11 | |
11 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.