GeeCON Prague 2014*
The moose likes XML and Related Technologies and the fly likes Unable to write back the content of XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Unable to write back the content of XML" Watch "Unable to write back the content of XML" New topic
Author

Unable to write back the content of XML

Tyagi Ashish
Greenhorn

Joined: Jul 12, 2009
Posts: 9
Hello I am trying to break-up big xml into small, I am using StAX, but I am not able to write back the content to the new file. Program is creating empty file

Please find my code

import java.io.File;
import java.io.FileReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;

public class Demo {

public static void main(String[] args) throws Exception {
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
xsr.nextTag(); // Advance to statements element

TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
File file = new File("out/" + xsr.getAttributeValue(null, "account") + ".xml");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new StAXSource(xsr), new StreamResult(file));

}
}

}





the input file contain

<statements>
<statement account="123">
</statement>
<statement account="456">
</statement>
</statements>



Please help me as soon as possible
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Your input is not well-formed XML. I'm surprised you aren't getting exceptions thrown. Or perhaps you are but are ignoring them?
Tyagi Ashish
Greenhorn

Joined: Jul 12, 2009
Posts: 9
Could you please help me How can I achieve that, thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

How can you achieve what? Making that XML well-formed? Not ignoring exceptions? Your question is not clear.
Tyagi Ashish
Greenhorn

Joined: Jul 12, 2009
Posts: 9
I am new with this thing but I have to implement this. I have tried with following XML as well but that is not working

<?xml version="1.0" encoding="UTF-8"?>
<statements>
<statement account="123">
Ashish
</statement>
<statement account="456">
Mohan
</statement>
</statements>


Could you please correct my code and provide the sample well-formed XML so that I can run and test that. I would really appreciate your help brother
Tyagi Ashish
Greenhorn

Joined: Jul 12, 2009
Posts: 9
Infact my original requirement said split the xml file into N small files where you provide N at run time, please let me know if you have code for that. You can use any of the sample XML you want. Thank you so much
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 520
    
    3
Your original code in post #1 should work for that specific xml. Your real code may not, can't say.
Tyagi Ashish
Greenhorn

Joined: Jul 12, 2009
Posts: 9
I am honestly saying, the code is running without exception and it is creating file as well but those files are empty and I am not sure how to fix that. Could you guys check if I have to add some more line in the code or I would have to import any jar file
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 520
    
    3
I am honestly saying, your original code in post #1 is working for that specific xml and it is creating the corresponding files with the requisite content.
Tyagi Ashish
Greenhorn

Joined: Jul 12, 2009
Posts: 9
ok so here I have modified the code for debug prospective and got the following exception

Code:



Content of Input.xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<import>
<dkoffer>Ashish</dkoffer>
<dkoffer>mohan</dkoffer>
<dkoffer>raj</dkoffer>
</import>

Exception:
The Source is dkoffer and the content is Ashish
Exception in thread "main" java.lang.IllegalStateException: XMLStreamReader must be in the START_DOCUMENT or START_ELEMENT state.
at javax.xml.transform.stax.StAXSource.<init>(Unknown Source)
at Demo.main(Demo.java:32)


Can you explain why it is not fetching the content of rest of the two element. Thanks !!!
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 520
    
    3
You choose to use StAX cursor api, you cannot move the cursor all the time and it cannot move back neither, that's its characteristic for efficiency which happens to be not coincide with yours.

[1] For splitting output

[2] If you want inspecting element text, don't rely on the output code lines.
Tyagi Ashish
Greenhorn

Joined: Jul 12, 2009
Posts: 9
Do I need to import any special jar for execution of [1] splitting the output (for ex. xalan.jar, jaxp-api.jar).

Now I am having following exception after adding (xalan.jar)
javax.xml.transform.TransformerException: Can't transform a Source of type javax.xml.transform.stax.StAXSource
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(Unknown Source)
at Demo2.main(Demo2.java:32)
Exception in thread "main" javax.xml.stream.XMLStreamException: A non-whitespace event found while calling nextTag.
at com.ibm.xml.xlxp.api.stax.msg.StAXMessageProvider.throwXMLStreamException(StAXMessageProvider.java:59)
at com.ibm.xml.xlxp.api.stax.XMLStreamReaderImpl.nextTag(XMLStreamReaderImpl.java:667)
at com.ibm.xml.xlxp.api.stax.XMLInputFactoryImpl$XMLStreamReaderProxy.nextTag(XMLInputFactoryImpl.java:197)
at Demo2.main(Demo2.java:28)
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 520
    
    3
If you're running jdk1.6, StAX should be included in its runtime jar, hence, no need to add anything special to the empty classpath to run. If it is below 1.6, you may need a separate download of stax: stax-1.2.0.jar for the latest RI, and included it in the classpath. But I would then be surprised on what basis you said in post #1 to have run the code without error?!
 
GeeCON Prague 2014
 
subject: Unable to write back the content of XML