This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes xml updations and deletions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "xml updations and deletions" Watch "xml updations and deletions" New topic
Author

xml updations and deletions

kalyan kiran
Greenhorn

Joined: Aug 21, 2001
Posts: 5
Hi,
I ma unable to delete and update a record in the xml file till now using jaxp. I am using a dom parser .
I have generated on xm file but name say m_layout.xml from the database values. the xml file looks like this.
<xml><records="3"><record row ="1"><header_id>head</header_id>
<lenght>3</length></record>
<record row="2"><header_id>jpsin</header_id><length>4</length>
</record>
<record row="3"><header_id>xxx</header_id><lenght>5</length>
</record>
</records></xml>
This is the structure of my xml. now suppose i want to modify first record in xml . how do i do that through dom and how do i write it back into file as what ever changes are made through dom they will be in memory only. i am psting the code what ever i have writeen please give me some feed back for tdeleting updating and appending records
code
----
package dfm;
import org.w3c.dom.*;
import com.sun.xml.tree.*;
import org.xml.sax.SAXException;
import java.io.*;
import java.util.*;
public class Parser
{
String filename="";
Document doc=null;
NodeList nodelist=null, nodelist1 = null,nodelist2 = null;
Node node=null, node1 = null,node2 = null;
NamedNodeMap map1=null;

public HashMap getHashMap()
{
int length = 0;
HashMap columnNames = new HashMap();
columnNames.put("*",filename.toUpperCase());
nodelist=doc.getElementsByTagName("record");
node = nodelist.item(0);
System.out.println(node.getNodeValue());
nodelist1 = node.getChildNodes();
length = nodelist1.getLength();

for(int j = 0 ; j < length ; j++)
{
node1 = nodelist1.item(j);
System.out.println(node1.getNodeName());
columnNames.put(node1.getNodeName(),new Integer(j+1));
}

return columnNames;
}
public Parser(String filename)
{
this.filename = filename;
try
{
InputStream instream=new FileInputStream(filename);
doc = XmlDocument.createXmlDocument(instream,false);
}
catch(Exception e){e.printStackTrace();}
}
public Vector getVector()
{
int len = 0,length = 0;
Vector data = new Vector();
int rowCnt = 0;
try
{
nodelist=doc.getElementsByTagName("record");
len=nodelist.getLength();
for(int i=0;i<len;i++)
{
node = nodelist.item(i);
nodelist1 = node.getChildNodes();
length = nodelist1.getLength();
String[] currentRow = new String[length+1];
for(int j = 0 ; j < length ; j++)
{

node1 = nodelist1.item(j);
currentRow[j+1] = node1.getChildNodes().item(0).getNodeValue();
}
data.addElement(currentRow);
rowCnt++;
}
}
catch(Exception e){e.printStackTrace();}
return data;
}
public Vector getLayoutids(String extendedFlag)
{
int len = 0,length = 0;
Vector data = new Vector();
try
{
nodelist=doc.getElementsByTagName("record");
len=nodelist.getLength();
for(int i=0;i<len;i++)
{
node = nodelist.item(i);
nodelist1 = node.getChildNodes();
node1 = nodelist1.item(1);
if(node1.getChildNodes().item(0).getNodeValue().equals(extendedFlag))
{
node1 = nodelist1.item(0);
data.addElement(node1.getChildNodes().item(0).getNodeValue());
}
}
}
catch(Exception e){e.printStackTrace();}
return data;
}
public Vector getLayoutidsAndRecordlens(String extendedFlag)
{
int len = 0,length = 0;
Vector data = new Vector();
try
{
nodelist=doc.getElementsByTagName("record");
len=nodelist.getLength();
for(int i=0;i<len;i++)
{
node = nodelist.item(i);
nodelist1 = node.getChildNodes();
node1 = nodelist1.item(1);
if(node1.getChildNodes().item(0).getNodeValue().equals(extendedFlag))
{
String[] res = new String[2];
node1 = nodelist1.item(0);
res[0]=node1.getChildNodes().item(0).getNodeValue();
node1 = nodelist1.item(2);
res[1]=node1.getChildNodes().item(0).getNodeValue();
data.addElement(res);
}
}
}
catch(Exception e){e.printStackTrace();}
return data;
}
public Vector getRecordlens(String extendedFlag)
{
int len = 0,length = 0;
Vector data = new Vector();
try
{
nodelist=doc.getElementsByTagName("record");
len=nodelist.getLength();
for(int i=0;i<len;i++)
{
node = nodelist.item(i);
nodelist1 = node.getChildNodes();
node1 = nodelist1.item(1);
if(node1.getChildNodes().item(0).getNodeValue().equals(extendedFlag))
{
node1 = nodelist1.item(2);
data.addElement(node1.getChildNodes().item(0).getNodeValue());
}
}
}
catch(Exception e){e.printStackTrace();}
return data;
}
public static void main(String args[])
{
if(args.length<1)
{
System.out.println("Give the file name");
}
else
{
Parser parse=new Parser(args[0]);
}
}


}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: xml updations and deletions
 
Similar Threads
Xml Parsing Question
XMLDocument.selectNodes
How to compare two huge xml files
Node.getNodeValue() is not parsing "
XML parsing using DOM????