Home » Modeling » EMF » Texo: how to load xml file?(Documentation or example needed)
Texo: how to load xml file? [message #858787] |
Fri, 27 April 2012 09:48 |
Olaf Burdziakowski Messages: 46 Registered: April 2012 |
Member |
|
|
I need to load data from xml file and store to database.
I used Texo to generate JAP anotated code + DAO based on my xsd file
Now I do not know how to load the data from xml file.
The example http: //wiki.eclipse.org/Texo/XML_and_XMI_Serialization is very poor.
Any full example please ...
[Updated on: Fri, 27 April 2012 11:18] Report message to a moderator
|
|
| | |
Re: Texo: how to load xml file [message #859093 is a reply to message #859036] |
Fri, 27 April 2012 12:59 |
Martin Taal Messages: 5468 Registered: July 2009 |
Senior Member |
|
|
Hi Olaf,
Yes your xml is invalid, you need namespace declarations, otherwise Texo/EMF doesn't know which type you are actually
trying to read. You can easily try out what Texo expects by doing this:
final ModelXMLSaver xmlSaver = new ModelXMLSaver();
xmlSaver.setObjects(Collections.singletonList(new MyListType());
final StringWriter sw = new StringWriter();
xmlSaver.setWriter(sw);
xmlSaver.write();
System.err.println(sw.toString());
Replace the new MyListType() with your Texo generated class for the List type, or use the generated Factory to create it.
Does this help?
gr. Martin
On 04/27/2012 02:25 PM, Olaf Burdziakowski wrote:
> Hi Martin,
> maybe I do not understand something but it do not work.
> What I did:
> Here is my xml file:(exmaple)
> <?xml version="1.0" encoding="UTF-8"?>
> <list>
> <file name="config_19700101000000.xml" valid_from="1970-01-01T00:00:00"/>
> </list>
> Here is my xsd:
> <?xml version="1.0" encoding="UTF-8"?>
> <xs:schema xmlns:xs="http: //www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
> <xs:complexType name="listType">
> <xs:sequence minOccurs="1" maxOccurs="unbounded">
> <xs:element name="file">
> <xs:complexType>
> <xs:attribute name="valid_from" type="xs:dateTime" use="required"/>
> <xs:attribute name="name" type="xs:string" use="required"/>
> </xs:complexType>
> </xs:element>
> </xs:sequence>
> </xs:complexType>
> <xs:element name="list" type="listType"/>
> </xs:schema>
>
> I generated with texo from eclipe menu: JPA anotated model code + DAO.
> I got some standard set of classes.
>
> Now I would like to load above xml file with your easy function:
>
> private static List<Object> readXML(final String xml) {
> final ModelXMLLoader xmlLoader = new ModelXMLLoader();
> xmlLoader.setLoadAsXMI(false);
> xmlLoader.setReader(new StringReader(xml));
> return xmlLoader.read();
> }
> As input there is string with loaded xml file. Now I got this error:
>
> Exception in thread "main" java.lang.IllegalStateException: org.eclipse.emf.ecore.resource.Resource$IOWrappedException:
> Package with uri 'null' not found. (, 1, 45)
> at org.eclipse.emf.texo.xml.ModelXMLLoader.read(ModelXMLLoader.java:95)
> at service.KCellPrices2.readXML(KCellPrices2.java:112)
> at service.KCellPrices2.main(KCellPrices2.java:73)
> Caused by: org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Package with uri 'null' not found. (, 1, 45)
> at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.handleErrors(XMLLoadImpl.java:83)
> at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:274)
> at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:802)
> at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.load(XMLResourceImpl.java:766)
> at org.eclipse.emf.texo.xml.ModelXMLLoader.read(ModelXMLLoader.java:91)
> ... 2 more
> Do you need more info?
--
With Regards, Martin Taal
Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
|
|
| | |
Re: Texo: how to load xml file [message #859472 is a reply to message #859298] |
Fri, 27 April 2012 16:58 |
Olaf Burdziakowski Messages: 46 Registered: April 2012 |
Member |
|
|
Hi Martin,
It helped me. Thank you. I did:
ModelPackage pModelPackage = CreConfigDirModelPackage.initialize();
Please add this to wiki.
Then writer has worked. But I got strange result:(xmlSaver.setSaveAsXMI(false))
<?xml version="1.0" encoding="UTF-8"?>
<list xmlns:xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="null file:/C:/Users/Olaf/workspace/KCellPrices2/model/cre_config_dir.xsd">
<file xsi:type="file_._type" name="config_19700101000000.xml" valid_from="2011-12-31T23:00:00.0Z"/>
</list>
Can I get somehow something closer to xml accepted by EMF ResourceLoader? To get something like this:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<file name="config_19700101000000.xml" valid_from="1970-01-01T00:00:00"/>
</list>
I mean I can add single reference to xml like file:/C:/Users/Olaf/workspace/KCellPrices2/model/cre_config_dir.xsd" but I would like to avoid xsi: in each xml element.
[Updated on: Fri, 27 April 2012 17:56] Report message to a moderator
|
|
| |
Re: Texo: how to load xml file [message #859694 is a reply to message #859597] |
Fri, 27 April 2012 19:14 |
Martin Taal Messages: 5468 Registered: July 2009 |
Senior Member |
|
|
Hi Olaf,
The strange thing is that you don't get a namespace. For example this a xml I get when converting the standard EMF
example to xml:
<?xml version="1.0" encoding="UTF-8"?>
<library:Library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:library="http://www.eclipse.org/emf/texo/test/model/samples/library" name="library">
<writers xsi:type="library:Writer" name="writer" books="#//@books.0"/>
<books xsi:type="library:Book" title="title" pages="27" category="ScienceFiction" author="#//@writers.0"/>
</library:Library>
It has a namespace and a namespace attribute.
gr. Martin
On 04/27/2012 08:07 PM, Martin Taal wrote:
> Hi Olaf,
> Except for the null schemalocation it is valid xml. What error does the EMF resource loader give?
>
> Anyway you can prevent the xsi:type and schemalocation like this:
> xmlSaver.getOptions().put(XMLResource.OPTION_SCHEMA_LOCATION, false);
> xmlSaver.getOptions().put(XMLResource.OPTION_SAVE_TYPE_INFORMATION, false);
>
> Let me know if the result works for you.
>
> gr. Martin
>
> On 04/27/2012 06:58 PM, Olaf Burdziakowski wrote:
>> Hi Martin,
>>
>> It helps me. I did:
>> ModelPackage pModelPackage = CreConfigDirModelPackage.initialize();
>> Please add this to wiki.
>>
>>
>> Then writer has worked. But I got strange result:(xmlSaver.setSaveAsXMI(false))
>> <?xml version="1.0" encoding="UTF-8"?>
>> <list xmlns:xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="null
>> file:/C:/Users/Olaf/workspace/KCellPrices2/model/cre_config_dir.xsd">
>> <file xsi:type="file_._type" name="config_19700101000000.xml" valid_from="2011-12-31T23:00:00.0Z"/>
>> </list>
>>
>> Can I get somehow something closer to xml accepted by EMF ResourceLoader? To get something like this:
>> <?xml version="1.0" encoding="UTF-8"?>
>> <list>
>> <file name="config_19700101000000.xml" valid_from="1970-01-01T00:00:00"/>
>> </list>
>
>
>
--
With Regards, Martin Taal
Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
|
|
|
Re: Texo: how to load xml file [message #859755 is a reply to message #859597] |
Fri, 27 April 2012 19:54 |
Olaf Burdziakowski Messages: 46 Registered: April 2012 |
Member |
|
|
Hi Martin,
my intention of writing to xml file was to learn what is required to read the xml file with Texo engine. So I tested:
String sExtra = "<list xmlns:xsi=\"http: //www.w3.org/2001/XMLSchema-instance\" xmlns:CreConfigDir=\"file:/C:/Users/Olaf/workspace/KCellPrices2/model/cre_config_dir.xsd\">";
xmi = writeXMI(pAL);
System.out.println(xmi);
xmi = xmi.replace("<list>", sExtra);
System.out.println(xmi);
List<Object> pList = readXML(xmi);
This works only when I set both functions (read and write) setSaveAsXMI(true) without any funny replacement. But I have files that are not XMI and I would like to read simple xml file with simple update of header element.
What is then required in xml to read it with List<Object> pList = readXML(xmi); when xmlLoader.setLoadAsXMI(false);?
Currently I still got an error:
Exception in thread "main" java.lang.IllegalStateException: org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Package with uri 'null' not found. (, 2, 150)
at org.eclipse.emf.texo.xml.ModelXMLLoader.read(ModelXMLLoader.java:95)
at service.KCellPrices2.readXML(KCellPrices2.java:158)
at service.KCellPrices2.main(KCellPrices2.java:80)
for such file:
<?xml version="1.0" encoding="UTF-8"?>
<list xmlns:xsi="http: //www.w3.org/2001/XMLSchema-instance" xmlns:CreConfigDir="file:/C:/Users/Olaf/workspace/KCellPrices2/model/cre_config_dir.xsd">
<file name="config_19700101000000.xml" valid_from="2011-12-31T23:00:00.0Z"/>
</list>
[Updated on: Fri, 27 April 2012 20:09] Report message to a moderator
|
|
|
Re: Texo: how to load xml file [message #859792 is a reply to message #859755] |
Fri, 27 April 2012 20:18 |
Martin Taal Messages: 5468 Registered: July 2009 |
Senior Member |
|
|
Hi Olaf,
Can you send me (mtaal@xxxxxxxx) the xsd file (or files) then I can try myself and debug, something is wrong with the
null uri. Not sure what/why.
gr. Maritn
On 04/27/2012 09:54 PM, Olaf Burdziakowski wrote:
> Hi Martin,
>
> my intention of writing to xml file was to learn what is required to read the xml file with Texo engine. So I tested:
>
> String sExtra = "<list xmlns:xsi=\"http: //www.w3.org/2001/XMLSchema-instance\"
> xmlns:CreConfigDir=\"file:/C:/Users/Olaf/workspace/KCellPrices2/model/cre_config_dir.xsd\">";
>
> xmi = writeXMI(pAL);
> System.out.println(xmi);
> xmi = xmi.replace("<list>", sExtra);
> System.out.println(xmi);
> List<Object> pList = readXML(xmi);
>
> This works only when I set both functions (read and write) have set setSaveAsXMI(true). But I have files that are not
> XMI and I would like to read simple xml file with simple update of header element.
>
> What is ten required in xml to read it with List<Object> pList = readXML(xmi); ?
> Currently I still got an error:
>
> Exception in thread "main" java.lang.IllegalStateException: org.eclipse.emf.ecore.resource.Resource$IOWrappedException:
> Package with uri 'null' not found. (, 2, 150)
> at org.eclipse.emf.texo.xml.ModelXMLLoader.read(ModelXMLLoader.java:95)
> at service.KCellPrices2.readXML(KCellPrices2.java:158)
> at service.KCellPrices2.main(KCellPrices2.java:80)
>
> for such file:
> <?xml version="1.0" encoding="UTF-8"?>
> <list xmlns:xsi="http: //www.w3.org/2001/XMLSchema-instance"
> xmlns:CreConfigDir="file:/C:/Users/Olaf/workspace/KCellPrices2/model/cre_config_dir.xsd">
> <file name="config_19700101000000.xml" valid_from="2011-12-31T23:00:00.0Z"/>
> </list>
>
>
--
With Regards, Martin Taal
Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
|
|
|
Re: Texo: how to load xml file [message #863154 is a reply to message #859792] |
Sun, 29 April 2012 09:46 |
Martin Taal Messages: 5468 Registered: July 2009 |
Senior Member |
|
|
Hi Olaf,
The solution is to set a namespace in your schema, so add these two attributes in xs:schema element:
xmlns="http://creconfigdir.model"
targetNamespace="http://creconfigdir.model"
The namespace also defines the package structure of your generated code, in this case the code will be generated in the
model.creconfigdir java package.
The testcase you passed works for me, the output/xml I get is this:
<?xml version="1.0" encoding="UTF-8"?>
<creconfigdir:list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:creconfigdir="http://creconfigdir.model">
<creconfigdir:file xsi:type="creconfigdir:file_._type" name="config_19700101000000.xml"
valid_from="2011-12-31T23:00:00.0Z"/>
</creconfigdir:list>
<?xml version="1.0" encoding="UTF-8"?>
<creconfigdir:list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:creconfigdir="http://creconfigdir.model">
<creconfigdir:file xsi:type="creconfigdir:file_._type" name="config_19700101000000.xml"
valid_from="2011-12-31T23:00:00.0Z"/>
</creconfigdir:list>
1
If you disable the type information:
<?xml version="1.0" encoding="UTF-8"?>
<creconfigdir:list xmlns:creconfigdir="http://creconfigdir.model">
<creconfigdir:file name="config_19700101000000.xml" valid_from="2011-12-31T23:00:00.0Z"/>
</creconfigdir:list>
<?xml version="1.0" encoding="UTF-8"?>
<creconfigdir:list xmlns:creconfigdir="http://creconfigdir.model">
<creconfigdir:file name="config_19700101000000.xml" valid_from="2011-12-31T23:00:00.0Z"/>
</creconfigdir:list>
1
gr. Martin
On 04/27/2012 10:18 PM, Martin Taal wrote:
> Hi Olaf,
> Can you send me (mtaal@xxxxxxxx) the xsd file (or files) then I can try myself and debug, something is wrong with the
> null uri. Not sure what/why.
>
> gr. Maritn
>
> On 04/27/2012 09:54 PM, Olaf Burdziakowski wrote:
>> Hi Martin,
>>
>> my intention of writing to xml file was to learn what is required to read the xml file with Texo engine. So I tested:
>>
>> String sExtra = "<list xmlns:xsi=\"http: //www.w3.org/2001/XMLSchema-instance\"
>> xmlns:CreConfigDir=\"file:/C:/Users/Olaf/workspace/KCellPrices2/model/cre_config_dir.xsd\">";
>>
>> xmi = writeXMI(pAL);
>> System.out.println(xmi);
>> xmi = xmi.replace("<list>", sExtra);
>> System.out.println(xmi);
>> List<Object> pList = readXML(xmi);
>>
>> This works only when I set both functions (read and write) have set setSaveAsXMI(true). But I have files that are not
>> XMI and I would like to read simple xml file with simple update of header element.
>>
>> What is ten required in xml to read it with List<Object> pList = readXML(xmi); ?
>> Currently I still got an error:
>>
>> Exception in thread "main" java.lang.IllegalStateException: org.eclipse.emf.ecore.resource.Resource$IOWrappedException:
>> Package with uri 'null' not found. (, 2, 150)
>> at org.eclipse.emf.texo.xml.ModelXMLLoader.read(ModelXMLLoader.java:95)
>> at service.KCellPrices2.readXML(KCellPrices2.java:158)
>> at service.KCellPrices2.main(KCellPrices2.java:80)
>>
>> for such file:
>> <?xml version="1.0" encoding="UTF-8"?>
>> <list xmlns:xsi="http: //www.w3.org/2001/XMLSchema-instance"
>> xmlns:CreConfigDir="file:/C:/Users/Olaf/workspace/KCellPrices2/model/cre_config_dir.xsd">
>> <file name="config_19700101000000.xml" valid_from="2011-12-31T23:00:00.0Z"/>
>> </list>
>>
>>
>
>
--
With Regards, Martin Taal
Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
|
|
| | | | |
Goto Forum:
Current Time: Wed Apr 24 16:24:19 GMT 2024
Powered by FUDForum. Page generated in 0.04122 seconds
|