View previous topic :: View next topic |
Author |
Message |
dubuila
Joined: 22 Aug 2006 Posts: 28
|
Posted: Tue Dec 19, 2006 8:22 am Post subject: SAX to file |
|
|
Hello,
Has someone a example of how to output the SAXReader information into a file ?
I need to filtrer input XML files to output limited ones.
Cheers,
Laurent. |
|
Back to top |
|
|
teqdruid
Joined: 11 May 2004 Posts: 390 Location: UMD
|
Posted: Wed Dec 20, 2006 5:45 am Post subject: |
|
|
The way to do this is to set up both an Reader and a Writer. Have your SAX callback client just pass through each call to the writer unless you want to filter it out.
Sorry- I don't have time to write example code for you... let me know if you have any more difficulties and I'll see what I can come up with later.
~John |
|
Back to top |
|
|
dubuila
Joined: 22 Aug 2006 Posts: 28
|
Posted: Wed Dec 20, 2006 6:49 am Post subject: Thanks |
|
|
Hi,
Thanks I'm near the coorect code. I missed the Reader and Writer part.
The client is the class based on the SAXhandler right ?
Cheers,
Laurent. |
|
Back to top |
|
|
teqdruid
Joined: 11 May 2004 Posts: 390 Location: UMD
|
|
Back to top |
|
|
dubuila
Joined: 22 Aug 2006 Posts: 28
|
Posted: Wed Dec 27, 2006 9:59 am Post subject: |
|
|
Hi,
Thanks. My problem is in the merge of the Writer into the Handler to replace the stdout by a Iwriter use...
Thanks,
Laurent. |
|
Back to top |
|
|
dubuila
Joined: 22 Aug 2006 Posts: 28
|
Posted: Wed Dec 27, 2006 10:46 am Post subject: |
|
|
Hello,
Better but not yet.
Two question :
- Need I to open the doc every time ?
- A assertion failed because I'm not in the Element in order to close it, any clue ?
As you could see I'm really a newbee here
module mango.test.sax.test1;
private import mango.convert.Unicode;
private import mango.io.Buffer;
private import mango.io.FileConduit;
private import mango.io.Stdout;
private import mango.io.Writer;
private import mango.io.Reader;
private import mango.xml.sax.DefaultSAXHandler;
private import mango.xml.sax.model.ISAXParser;
private import mango.xml.sax.model.ISAXHandler;
private import mango.xml.sax.parser.teqXML;
private import mango.text.model.UniString;
private import mango.text.String;
// make an char as an alias of T
alias char T;
// cast String of T as a Utf8 String by template
private alias StringT!(T) Utf8String;
void main()
{
readerTest1();
//writerTest1();
}
/**
Read the file by a OutputHandler
*/
int readerTest1()
{
ISAXReader!(T) reader = new TeqXMLReader!(T)(120);
FileConduit file = new FileConduit("c:/coding/hyridia/test/xml/sample.xml", FileStyle.ReadExisting);
//ConsoleOutputHandler handler = new ConsoleOutputHandler();
FileOutputHandler handler = new FileOutputHandler();
reader.parse(file, handler);
return 0;
}
void writerTest1()
{
TeqXMLWriter xw = new TeqXMLWriter();
auto TextFileConduit fc = new TextFileConduit("c:/coding/hyridia/test/xml/sample_out.xml", FileStyle.ReadWriteCreate);
xw.startDocument(new Buffer(fc), Unicode.UTF_8);
xw.processingInstruction(new Utf8String("processing"), new Utf8String("instruction"));
xw.startElement(new Utf8String("mainElement"));
xw.addAttribute(new Utf8String("key"), new Utf8String("value"));
xw.characterData(new Utf8String("Data"), CDataStatus.All);
xw.endElement(new Utf8String("mainElement"));
xw.endDocument();
}
/**
Outputs the data to the file.
*/
class FileOutputHandler: DefaultSAXHandler!(T) {
private TeqXMLWriter xw;
private TextFileConduit fc;
this() {
xw = new TeqXMLWriter();
fc = new TextFileConduit("c:/coding/hyridia/test/xml/sample_out_3.xml", FileStyle.ReadWriteCreate);
}
~this(){
}
void processingInstruction(String target, String data) {
xw.startDocument(new Buffer(fc), Unicode.UTF_8);
xw.processingInstruction(new Utf8String(target.utf8()), new Utf8String(data.utf8()));
xw.endDocument();
}
void startElement(String name) {
xw.startDocument(new Buffer(fc), Unicode.UTF_8);
xw.startElement(new Utf8String(name.utf8()));
xw.endDocument();
}
void addAttribute(String key, String value) {
xw.startDocument(new Buffer(fc), Unicode.UTF_8);
xw.addAttribute(new Utf8String(key.utf8()), new Utf8String(value.utf8()));
xw.endDocument();
}
void endElement(String name) {
xw.startDocument(new Buffer(fc), Unicode.UTF_8);
xw.endElement(new Utf8String(name.utf8()));
xw.endDocument();
}
void characterData (String data, CDataStatus status) {
xw.startDocument(new Buffer(fc), Unicode.UTF_8);
xw.characterData(new Utf8String(data.utf8()), CDataStatus.All);
xw.endDocument();
}
void comment(String text) {
xw.startDocument(new Buffer(fc), Unicode.UTF_8);
xw.comment(new Utf8String(text.utf8()));
xw.endDocument();
}
} |
|
Back to top |
|
|
teqdruid
Joined: 11 May 2004 Posts: 390 Location: UMD
|
Posted: Wed Dec 27, 2006 5:25 pm Post subject: |
|
|
You've got it right in your writerTest1, but your output in the handler is no good. The XMLWriter ensures that the output is valid, so you can only output valid XML. Things like just attr="something" is not valid XML in and of itself. So, you can only call xv.addAttribute after an xv.startElement and before you've outputted any CDATA or another element inside of it. What you might try doing is running xv.startDocument only in void startDocument and the same for endDocument in endDocument.
~John |
|
Back to top |
|
|
dubuila
Joined: 22 Aug 2006 Posts: 28
|
Posted: Thu Dec 28, 2006 3:02 am Post subject: |
|
|
Thanks,
Will test.
Cheers,
Laurent |
|
Back to top |
|
|
dubuila
Joined: 22 Aug 2006 Posts: 28
|
Posted: Mon Jan 08, 2007 10:00 am Post subject: |
|
|
Hi,
Thanks.
Two new cases here.
Can I use unique letter tag like : <A><B>1</B></B> ? Now the code (following) ask me and attributee =
Second one :
Now I get from :
<Ab>
<Ac>Some text.</Ac>
<Ad att="Some text."/>
</Ab>
The output :
<?xml version="1.0" encoding="UTF-8"?>
<Ab>
<Ac>
Some text.
</Ac>
<Ad att="Some text."/>
/>
And the parser is implemented out of the box :
class FileOutputHandler: DefaultSAXHandler!(T) {
private TeqXMLWriter xw;
private TextFileConduit fc;
this()
{
xw = new TeqXMLWriter();
fc = new TextFileConduit("c:/coding/hyridia/crk/test/output/sample_out.xml", FileStyle.ReadWriteCreate);
}
~this(){}
void startDocument()
{
// Start Document create the line <?xml version="" encoding=""?>
xw.startDocument(new Buffer(fc), Unicode.UTF_8);
}
void endDocument()
{
xw.endDocument();
}
void processingInstruction(String target, String data)
{
// None at this time.
}
void startElement(String name)
{
xw.startElement(new Utf8String(name.utf8()));
}
void addAttribute(String key, String value)
{
xw.addAttribute(new Utf8String(key.utf8()), new Utf8String(value.utf8()));
}
void endElement(String name)
{
xw.endElement(new Utf8String(name.utf8()));
}
void characterData (String data, CDataStatus status)
{
xw.characterData(new Utf8String(data.utf8()), CDataStatus.All);
}
void comment(String text)
{
xw.comment(new Utf8String(text.utf8()));
}
}
Need I to have a look to the handler event ? In tag.. ?
Cheers,
Laurent. |
|
Back to top |
|
|
teqdruid
Joined: 11 May 2004 Posts: 390 Location: UMD
|
Posted: Thu Jan 11, 2007 12:25 am Post subject: |
|
|
dubuila wrote: |
Now I get from :
<Ab>
<Ac>Some text.</Ac>
<Ad att="Some text."/>
</Ab>
The output :
<?xml version="1.0" encoding="UTF-8"?>
<Ab>
<Ac>
Some text.
</Ac>
<Ad att="Some text."/>
/>
|
That was a bug. It's fixed in SVN now, so svn update and and it should go away.
~John |
|
Back to top |
|
|
dubuila
Joined: 22 Aug 2006 Posts: 28
|
Posted: Tue Jan 23, 2007 10:15 am Post subject: |
|
|
Thanks John,
Sorry for the delay in my test.
Cheers,
Laurent. |
|
Back to top |
|
|
|