The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-14-2009
c0mrade c0mrade is offline
Registered User
  
 

Join Date: May 2008
Posts: 35
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);
    }
  }
}
The code above is working but when I want to change it to output to a file like : Result dest = new StreamResult(new File("foo.xml")); or Result dest = new StreamResult(new FileOutputStream("foo.xml")); its not working, any indications why ?

Thank you
  #2 (permalink)  
Old 10-14-2009
varontron varontron is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 110
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);
  #3 (permalink)  
Old 10-15-2009
c0mrade c0mrade is offline
Registered User
  
 

Join Date: May 2008
Posts: 35
tnx for your answer .. I've writen removeallnodes method which goes trough every child of a element I wanna get rid of and it works now as it should
  #4 (permalink)  
Old 10-15-2009
jhoffler jhoffler is offline
Registered User
  
 

Join Date: Oct 2009
Location: Charlotte, NC
Posts: 1
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>
This XSL removes the need for this code:
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();
A bonus of the XSL approach is that you don't even have to write a Java program. On Linux run
Code:
xsltproc --output destination.xml prune.xsl source.xml
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:11 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0