You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlbeans-user@xml.apache.org by Arek Stryjski <de...@krzyzowki.net.pl> on 2004/07/26 17:23:49 UTC

binding XML fragment from DOM Node

Hi,

Is it possible to bind XML fragment from DOM Node (Element) to XmlObject?

Loading whole XML document with code like this:
Document domDoc = builder.parse(f);
MyXMLDocument doc = MyXMLDocument.Factory.parse(domDoc);
is working perfectly well.

But loading only part of this document like:
Document domDoc = builder.parse(f);
MyXMLBean root = MyXMLBean.Factory.parse(domDoc.getFirstChild());
fail.
In my opinion this code should work (if given Node is really of expected type).
All XmlObjects have Factory.parse(Node) methods not Factory.parse(Document).
Also then calling, this method is not throwing Exception that given some massage
like: Node has wrong DOM NodeType.

If I remember well XmlBeans work with XML fragments, right?

Maybe I do some stupid mistake, or I should set something extra (possibly with
XmlOptions)?

Thank you for any help.
Arek

PS
If someone would like to look deeper at it I made some test program. 

DOMTestMain.java:

package tests.dom;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.xmlbeans.XmlException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import com.accountz.xmlbeans.table.simple.SimpleTableModelBean;
import com.accountz.xmlbeans.table.simple.SimpleTableModelDocument;

/**
 *
 *
 */
public class DOMTestMain
{
    private DocumentBuilder builder;

    public DOMTestMain() throws Exception
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        builder = factory.newDocumentBuilder();

        File f = new File("sample.xml");
        bind(f);
        dom(f);
        domDoc(f);
    }

    private void bind(File f) throws IOException, XmlException
    {
        System.out.println("bind()");
        SimpleTableModelDocument doc = SimpleTableModelDocument.Factory.parse(f);
        System.out.println(doc.getSimpleTableModel().xmlText());
        System.out.println("size="+doc.getSimpleTableModel().sizeOfColumnArray());
        System.out.println("\n");
    }

    private void dom(File f) throws FactoryConfigurationError,
ParserConfigurationException, IOException, SAXException, XmlException
    {
        System.out.println("dom()");
        Document domDoc = builder.parse(f);

        SimpleTableModelBean root =
SimpleTableModelBean.Factory.parse(domDoc.getFirstChild());
        System.out.println(root.xmlText());
        System.out.println("size="+root.sizeOfColumnArray());
        System.out.println("\n");
    }

    private void domDoc(File f) throws FactoryConfigurationError,
ParserConfigurationException, IOException, SAXException, XmlException
    {
        System.out.println("domDoc()");
        Document domDoc = builder.parse(f);

        SimpleTableModelDocument doc =
SimpleTableModelDocument.Factory.parse(domDoc);
        System.out.println(doc.getSimpleTableModel().xmlText());
        System.out.println("size="+doc.getSimpleTableModel().sizeOfColumnArray());
        System.out.println("\n");
    }

    public static void main(String[] args) throws Exception
    {
        new DOMTestMain();
    }

}

------------------------------------------------------------------------
sample-table.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.accountz.com/xmlbeans/table/simple"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:stm="http://www.accountz.com/xmlbeans/table/simple"
        elementFormDefault="qualified"
        attributeFormDefault="unqualified">

<element name="simple-table-model" type="stm:SimpleTableModelBean"/>

<complexType name="SimpleTableModelBean">
    <sequence>
        <element name="column" type="stm:Column" minOccurs="0"
maxOccurs="unbounded"/>
    </sequence>
</complexType>

<complexType name="Column">
    <sequence>
        <element name="header" type="xs:string"/>
        <element name="cel" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
    </sequence>
</complexType>

</schema>

------------------------------------------------------------------------
simple.xml:

<sim:simple-table-model xmlns:sim="http://www.accountz.com/xmlbeans/table/simple">
   <sim:column>
       <sim:header>letters</sim:header>
       <sim:cel>a</sim:cel>
       <sim:cel>b</sim:cel>
       <sim:cel>c</sim:cel>
       <sim:cel>d</sim:cel>
       <sim:cel>e</sim:cel>
   </sim:column>
   <sim:column>
       <sim:header>numbers</sim:header>
       <sim:cel>1</sim:cel>
       <sim:cel>2</sim:cel>
       <sim:cel>3</sim:cel>
       <sim:cel>4</sim:cel>
       <sim:cel>5</sim:cel>
   </sim:column>
   <sim:column>
       <sim:header>roman</sim:header>
       <sim:cel>I</sim:cel>
       <sim:cel>II</sim:cel>
       <sim:cel>III</sim:cel>
       <sim:cel>IV</sim:cel>
       <sim:cel>V</sim:cel>
   </sim:column>
</sim:simple-table-model>

------------------------------------------------------------------------
System.out:

$ java -jar dom-test.jar
bind()
<xml-fragment xmlns:sim="http://www.accountz.com/xmlbeans/table/simple">
   <sim:column>
       <sim:header>letters</sim:header>
       <sim:cel>a</sim:cel>
       <sim:cel>b</sim:cel>
       <sim:cel>c</sim:cel>
       <sim:cel>d</sim:cel>
       <sim:cel>e</sim:cel>
   </sim:column>
   <sim:column>
       <sim:header>numbers</sim:header>
       <sim:cel>1</sim:cel>
       <sim:cel>2</sim:cel>
       <sim:cel>3</sim:cel>
       <sim:cel>4</sim:cel>
       <sim:cel>5</sim:cel>
   </sim:column>
   <sim:column>
       <sim:header>roman</sim:header>
       <sim:cel>I</sim:cel>
       <sim:cel>II</sim:cel>
       <sim:cel>III</sim:cel>
       <sim:cel>IV</sim:cel>
       <sim:cel>V</sim:cel>
   </sim:column>
</xml-fragment>
size=3


dom()
<sim:simple-table-model xmlns:sim="http://www.accountz.com/xmlbeans/table/simple">
   <sim:column>
       <sim:header>letters</sim:header>
       <sim:cel>a</sim:cel>
       <sim:cel>b</sim:cel>
       <sim:cel>c</sim:cel>
       <sim:cel>d</sim:cel>
       <sim:cel>e</sim:cel>
   </sim:column>
   <sim:column>
       <sim:header>numbers</sim:header>
       <sim:cel>1</sim:cel>
       <sim:cel>2</sim:cel>
       <sim:cel>3</sim:cel>
       <sim:cel>4</sim:cel>
       <sim:cel>5</sim:cel>
   </sim:column>
   <sim:column>
       <sim:header>roman</sim:header>
       <sim:cel>I</sim:cel>
       <sim:cel>II</sim:cel>
       <sim:cel>III</sim:cel>
       <sim:cel>IV</sim:cel>
       <sim:cel>V</sim:cel>
   </sim:column>
</sim:simple-table-model>
size=0


domDoc()
<xml-fragment xmlns:sim="http://www.accountz.com/xmlbeans/table/simple">
   <sim:column>
       <sim:header>letters</sim:header>
       <sim:cel>a</sim:cel>
       <sim:cel>b</sim:cel>
       <sim:cel>c</sim:cel>
       <sim:cel>d</sim:cel>
       <sim:cel>e</sim:cel>
   </sim:column>
   <sim:column>
       <sim:header>numbers</sim:header>
       <sim:cel>1</sim:cel>
       <sim:cel>2</sim:cel>
       <sim:cel>3</sim:cel>
       <sim:cel>4</sim:cel>
       <sim:cel>5</sim:cel>
   </sim:column>
   <sim:column>
       <sim:header>roman</sim:header>
       <sim:cel>I</sim:cel>
       <sim:cel>II</sim:cel>
       <sim:cel>III</sim:cel>
       <sim:cel>IV</sim:cel>
       <sim:cel>V</sim:cel>
   </sim:column>
</xml-fragment>
size=3






- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-user-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/


newDomNode(XmlOptions) (was: binding XML fragment from DOM Node)

Posted by Arek Stryjski <de...@krzyzowki.net.pl>.
Hi,

Sorry if I put too much frustration in my last letter. I still think XmlBeans is
the best binding framework. If you think that using DOM is stupid, I agree, but
unfortunately I need to combine DOM and XmlBeans in my project, as other
packages use DOM.

I read documentation for org.apache.xmlbeans.XmlTokenSource.newDomNode() and now
I understand that you use only Document and DocumentFragment, but as you need
one return type you must use Node. The same principle is used in parse(Node)
method I believe. 
I still think there should be some assertion error thrown if I pass Element to
this method, but... it is not something you will call bug.

However now I come across other behavior which is strange for me. 
Then I use code like this:
1  XmlOptions op = new XmlOptions();
2  op.setSaveAggresiveNamespaces();
3  op.setSaveOuter();
4
5  SimpleTableModelDocument doc = impleTableModelDocument.Factory.newInstance();
6  SimpleTableModelBean bean = doc.addNewSimpleTableModel();     
7  Node n = bean.newDomNode(op);
8
9  System.out.println("bean="+bean.xmlText());   
10 System.out.println("node="+n.getFirstChild());

the system output as expected is:
   bean=<xml-fragment/>
   node=<sim:simple-table-model xmlns:sim="http://xmlbeans/table/simple" />

But if I replace lines 5-7 with:
   SimpleTableModelBean bean = SimpleTableModelBean.Factory.newInstance();        
   Node n = bean.newDomNode(op);

I get in output:
   bean=<xml-fragment></xml-fragment>
   node=null

I looks like now DocumentFragment is empty. But why? It should contain one
element in my opinion. Or I'm wrong again?

The most inexplicable behavior I got then on line 7 I place:
   Node n = bean.newDomNode();

it case:
Exception in thread "main" java.lang.NullPointerException
 at org.apache.xmlbeans.impl.store.Saver$DomSaver.emitContainer(Saver.java:4514)
 at org.apache.xmlbeans.impl.store.Saver.processContainer(Saver.java:775)
 at org.apache.xmlbeans.impl.store.Saver.process(Saver.java:518)
 at org.apache.xmlbeans.impl.store.Saver$DomSaver.exportDom(Saver.java:4461)
 at org.apache.xmlbeans.impl.store.Cursor.newDomNode(Cursor.java:2954)
 at org.apache.xmlbeans.impl.values.XmlObjectBase.newDomNode(XmlObjectBase.java:154)
 at org.apache.xmlbeans.impl.values.XmlObjectBase.newDomNode(XmlObjectBase.java:151)
...
on this line. 

So it looks like my problems come from wrong settings in XmlOptions passed to
newDomNode(); There I could read more about usage of XmlOptions in context of
DOM import/export. 

Thanks for any help.

Arek



- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-user-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/


newDomNode(XmlOptions) (was: binding XML fragment from DOM Node)

Posted by Arek Stryjski <de...@krzyzowki.net.pl>.
Hi,

Sorry if I put too much frustration in my last letter. I still think XmlBeans is
the best binding framework. If you think that using DOM is stupid, I agree, but
unfortunately I need to combine DOM and XmlBeans in my project, as other
packages use DOM.

I read documentation for org.apache.xmlbeans.XmlTokenSource.newDomNode() and now
I understand that you use only Document and DocumentFragment, but as you need
one return type you must use Node. The same principle is used in parse(Node)
method I believe. 
I still think there should be some assertion error thrown if I pass Element to
this method, but... it is not something you will call bug.

However now I come across other behavior which is strange for me. 
Then I use code like this:
1  XmlOptions op = new XmlOptions();
2  op.setSaveAggresiveNamespaces();
3  op.setSaveOuter();
4
5  SimpleTableModelDocument doc = impleTableModelDocument.Factory.newInstance();
6  SimpleTableModelBean bean = doc.addNewSimpleTableModel();     
7  Node n = bean.newDomNode(op);
8
9  System.out.println("bean="+bean.xmlText());   
10 System.out.println("node="+n.getFirstChild());

the system output as expected is:
   bean=<xml-fragment/>
   node=<sim:simple-table-model xmlns:sim="http://xmlbeans/table/simple" />

But if I replace lines 5-7 with:
   SimpleTableModelBean bean = SimpleTableModelBean.Factory.newInstance();        
   Node n = bean.newDomNode(op);

I get in output:
   bean=<xml-fragment></xml-fragment>
   node=null

I looks like now DocumentFragment is empty. But why? It should contain one
element in my opinion. Or I'm wrong again?

The most inexplicable behavior I got then on line 7 I place:
   Node n = bean.newDomNode();

it case:
Exception in thread "main" java.lang.NullPointerException
 at org.apache.xmlbeans.impl.store.Saver$DomSaver.emitContainer(Saver.java:4514)
 at org.apache.xmlbeans.impl.store.Saver.processContainer(Saver.java:775)
 at org.apache.xmlbeans.impl.store.Saver.process(Saver.java:518)
 at org.apache.xmlbeans.impl.store.Saver$DomSaver.exportDom(Saver.java:4461)
 at org.apache.xmlbeans.impl.store.Cursor.newDomNode(Cursor.java:2954)
 at org.apache.xmlbeans.impl.values.XmlObjectBase.newDomNode(XmlObjectBase.java:154)
 at org.apache.xmlbeans.impl.values.XmlObjectBase.newDomNode(XmlObjectBase.java:151)
...
on this line. 

So it looks like my problems come from wrong settings in XmlOptions passed to
newDomNode(); There I could read more about usage of XmlOptions in context of
DOM import/export. 

Thanks for any help.

Arek



- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-user-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/