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 tags writing 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 tags writing" Watch "xml tags writing" New topic
Author

xml tags writing

subrahm puvvada
Greenhorn

Joined: Aug 23, 2005
Posts: 14
<?xml version="1.0" standalone="no"?>
<sf>
<URI ID="1">
<Res>java</Res>
<Fi>
<Name>core</Name>
<IsEncrypted>NO</IsEncrypted>
</Fi>
<Fi>
<Name>adv</Name>
<IsEncrypted>NO</IsEncrypted>
</Fi>
<Fi>
<Name>j2ee</Name>
<IsEncrypted>NO</IsEncrypted>
</Fi>
<Fi>
<Name>j2m0</Name>
<IsEncrypted>NO</IsEncrypted>
</Fi>
</URI>
<URI ID="1">
<Res>asp</Res>
<Fi>
<Name>aa0</Name>
<IsEncrypted>NO</IsEncrypted>
</Fi>
<Fi>
<Name>bb</Name>
<IsEncrypted>NO</IsEncrypted>
</Fi>
<Fi>
<Name>cc</Name>
<IsEncrypted>NO</IsEncrypted>
</Fi>
</URI>
</Sf>
this is my xml file
byusing jsp u have to populate the values in the combo.when u select avalue in the combo that correspoding values should be displayed in a table.the encryption values should have checkbox .that i have done it.now my problem is when u check some of the values and click submit the new values are submitted into the xml file.suppose when u check 2 checkboxes in encryption fields,the values yes should be updated in the xml file.

Iam sending my jsp file also


<%@ page import = "javax.xml.parsers.DocumentBuilderFactory"%>
<%@ page import = "org.w3c.dom.*"%>
<%@ page import = "javax.xml.parsers.DocumentBuilder"%>
<%@ page import = "org.xml.sax.SAXException"%>
<%@ page import = "org.xml.sax.SAXParseException"%>
<%@ page import = "javax.xml.parsers.*"%>
<%@ page import = "java.io.ByteArrayOutputStream,java.util.Vector,javax.xml.transform.Transformer,javax.xml.transform.TransformerFactory,javax.xml.transform.dom.DOMSource,javax.xml.transform.stream.StreamResult"%>



<html>
<head>
<script language="javascript">
function submitForm() {

alert(document.samp.sel.value);
var filev = document.samp.sel.value;
document.samp.action="parse.jsp?selValue="+filev;
document.samp.submit();
}
</script>
</head>

<%!
Document xmlDocument = null;

public void createXMLDocument(String sFileName)
{
DocumentBuilder documentBuilder = null;
DocumentBuilderFactory documentBuilderFactory = null;

try
{
documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilder = documentBuilderFactory.newDocumentBuilder();
xmlDocument = documentBuilder.parse(sFileName);
}
catch (Exception e)
{
e.printStackTrace();

}
}


public Element getRootNode()
{
return xmlDocument.getDocumentElement();
}



public String getElementValue(Element element)
{
Node node = element.getFirstChild();
if (node != null && node.getNodeType() == Node.TEXT_NODE)
{
return node.getNodeValue();
}
return null;
}


public Element getElement(Element element, String name)
{

NodeList nL = element.getElementsByTagName(name);

for (int i = 0; i < nL.getLength(); i++)
{
Node n = nL.item(i);
if (((n.getParentNode()).getNodeName())
.equalsIgnoreCase((element.getTagName())))
{
return (Element) nL.item(i);
}
}
if (nL.getLength() == 0)
{
throw new RuntimeException("Element <" + name + "> not found");
} //end of if
return (Element) nL.item(0);

}


public Element getBodyElement(String name)
{
NodeList nL = xmlDocument.getElementsByTagName(name);
if (nL.getLength() == 0)
{
throw new RuntimeException("Element <" + name + "> not found");
} //end of if
return (Element) nL.item(0);
}


public Vector getChildElements(Element parentElement)
{
NodeList nL = parentElement.getChildNodes();
if (nL.getLength() == 0)
{
throw new RuntimeException("No child elements found");
}
Vector vElements = new Vector();
for (int i = 0; i < nL.getLength(); i++)
{
Node node = nL.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
vElements.add((Element) node);
}
}
return vElements;
}


public Vector getChildElements(Element parentElement, String name)
{
NodeList nL = parentElement.getChildNodes();
if (nL.getLength() == 0)
{
throw new RuntimeException("No child elements found");
} //end of if
Vector vElements = new Vector();
for (int i = 0; i < nL.getLength(); i++)
{
Node node = nL.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
String nodeName = trimNS(node.getNodeName());
if (nodeName.equalsIgnoreCase(name))
{
vElements.add((Element) node);
}
} //end of if
}
return vElements;

} //end of getElements(Element, String)


public Element getFirstChildElement(Element element)
{
NodeList nL = element.getChildNodes();
for (int i = 0; i < nL.getLength(); i++)
{
Node node = nL.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
return (Element) node;
}
} //end of for

throw new RuntimeException(
element.getTagName() + " does not have " + "any child elements.");
}


public String trimNS(String nameWithNS)
{
int index = nameWithNS.indexOf(":");
if (index != -1)
{
nameWithNS = nameWithNS.substring(index + 1);
}
return nameWithNS;
} //end of trimNS(String)

public String writeXML()
{
try
{
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(xmlDocument);
StreamResult result = new StreamResult(new ByteArrayOutputStream());
transformer.transform(source, result);
return result.getOutputStream().toString();
}
catch (Exception e)
{
throw new RuntimeException("Unable to convert the XMLDocument to String");
}
}



%>






<body bgcolor="red">
<B>
<form name=samp method=post>
<center><select name="sel" onChange="submitForm()">
<option value="0" selected>-select memberfile-</option>
<%

this.createXMLDocument("d:\\puvvada\\jsp\\ll\\SecureFileconfig.xml");
StringBuffer resources = new StringBuffer();
StringBuffer fields = new StringBuffer();
StringBuffer enc = new StringBuffer();

//System.out.println("xml...file..."+this.writeXML());
Element rootElement = this.getRootNode();


System.out.println("root element..."+rootElement.getNodeName());



Vector vv = this.getChildElements(rootElement);
System.out.println("Total Members.."+vv.size());

for(int i=0;i<vv.size();i++)
{
Vector uris = this.getChildElements((Element)vv.elementAt(i));
System.out.println("subvector..size.."+uris.size());
for(int j=0;j<uris.size();j++)
{
if(j==0)
{
Element ee = (Element)uris.elementAt(0);
%>

<option value=<%=this.getElementValue(ee)%>><%=this.getElementValue(ee)%></option>
<%



}
else
{
Vector last = this.getChildElements((Element)uris.elementAt(j));
fields.append(this.getElementValue((Element)last.get(0)));
fields.append("[[]]");
enc.append(this.getElementValue((Element)last.get(1)));
enc.append("[[]]");
}


}

}


%>





</select>
</center>
</form>
<center>
<table width=50% height=50%>
<tr><th>Sno</th><th>FieldName</th><th>EncryStatus</th></tr>
<%
String reqvalue = request.getParameter("selValue").toString();
System.out.println(reqvalue);
if(reqvalue !=null)
{
if(!(reqvalue.equals("0")))
{

for(int i=0;i<vv.size();i++)
{
Vector uris = this.getChildElements((Element)vv.elementAt(i));
System.out.println("subvector..size.."+uris.size());
for(int j=1;j<uris.size();j++)
{

Element ee = (Element)uris.elementAt(0);

if(this.getElementValue(ee).equals(reqvalue))
{
Vector last = this.getChildElements((Element)uris.elementAt(j));
%>

<tr><td><%=j%></td><td><%=this.getElementValue((Element)last.get(0))%></td><td><%=this.getElementValue((Element)last.get(1))%><input type=checkbox></td></tr>


<%}

}

}





}
}
%>
</table>

</center>

</html>
please send this .atlest tellme how to updatethe value in the xml file
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
Hello Subrahm,

Originally posted by subrahm puvvada:
now my problem is when u check some of the values and click submit the new values are submitted into the xml file.suppose when u check 2 checkboxes in encryption fields,the values yes should be updated in the xml file.


function submitForm() {

alert(document.samp.sel.value);
var filev = document.samp.sel.value;
document.samp.action="parse.jsp?selValue="+filev;
document.samp.submit();
}


Lets see what you have?

You have the old xml file in the server.

You have the value that has to be updated to the xml file

What next?

1. parse the xml file and traverse to the element you want to add the new text.

2. Remove the old text node

3. Create a new Text node with the new value submitted by the form. "selValue" in this case

4. Append this text node to the Element Node.

5. Serialise the xml document

Sounds complicated? then it is time to try JDOM

Regards,
[ September 14, 2005: Message edited by: Rajagopal Manohar ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: xml tags writing
 
Similar Threads
java.lang.string
text node value null...
Dom parser for XML file
Parsing XML doc
xml parsing with dom