xml-xmlbeans-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Arek Stryjski <develo...@krzyzowki.net.pl>
Subject binding XML fragment from DOM Node
Date Mon, 26 Jul 2004 15:23:49 GMT
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/


Mime
View raw message