![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Mac OS X 10.5: Unable to launch Java Web Start JNLP file after installing Java Update | iBot | OS X Support RSS | 0 | 08-31-2009 05:30 PM |
| how to get result from exe file using java | vip_a1 | High Level Programming | 1 | 05-24-2009 10:34 PM |
| Interpreting java output stream as system commands in Solaris | EugeneG | UNIX for Dummies Questions & Answers | 1 | 07-18-2007 06:45 PM |
| lslpp output for Java package | Student37 | UNIX for Dummies Questions & Answers | 1 | 06-05-2005 05:09 PM |
| run a java file on UNIX? | leotopia | UNIX for Dummies Questions & Answers | 3 | 07-18-2003 12:44 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Output to a file in java
I wanna remove element from xml file, when I run the following code I get the correct output when I'm outputing to console but when I'm trying to ouptup the same code to a file I get the same / original xml file without removed element, here is the code :
Code:
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class RemoveBlock {
static public void main(String[] arg) {
try{
String xmlFile = "Management.xml";
File file = new File(xmlFile);
String remElement = "Physical_Order_List_Array";
if (file.exists()){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer tFormer = tFactory.newTransformer();
Element element = (Element)doc.getElementsByTagName(remElement).item(0);
// Remove the node
element.getParentNode().removeChild(element);
// Normalize the DOM tree to combine all adjacent nodes
doc.normalize();
Source source = new DOMSource(doc);
Result dest = new StreamResult(System.out);
tFormer.transform(source, dest);
// System.out.println();
// saveStringToFile("emir.xml", "dsds"); -> write method
}
else{
System.out.println("File not found!");
}
}
catch (Exception e){
System.err.println(e);
System.exit(0);
}
}
}
Thank you |
|
||||
|
not entirely sure, but I think you can't transform a DOM Document into a stream result without serializing it first.
look at the docs for org.apache.xml.serializer.DOMSerializer; here's some code fragments that may be helpful. Code:
public static Serializer getSerializer()
{
Properties props = OutputPropertiesFactory.getDefaultMethodProperties("xml");
ser = SerializerFactory.getSerializer(props);
return ser;
}
os = new ByteArrayOutputStream();
ros = new ByteArrayOutputStream();
getSerializer().setOutputStream(os);
DOMSerializer dser = getSerializer().asDOMSerializer();
dser.serialize(document);
is = new ByteArrayInputStream(os.toByteArray());
source = new StreamSource(is);
result = new StreamResult(ros);
|
|
||||
|
Try XSL for deleting elements
Since your code does a transform anyway, consider using the following XSLT:
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- Passthru for XML tags -->
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="Physical_Order_List_Array"/>
</xsl:stylesheet>
Code:
Element element = (Element)doc.getElementsByTagName(remElement).item(0);
// Remove the node
element.getParentNode().removeChild(element);
// Normalize the DOM tree to combine all adjacent nodes
doc.normalize();
Code:
xsltproc --output destination.xml prune.xsl source.xml |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|