You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Matthias Wessendorf <mw...@pironet-ndh.com> on 2005/06/08 17:10:11 UTC

problems on modifing XML files with XML_BEANS

Hi,

I am using XmlBeans for reading content of xml files. That works fine and all works like I expected.

But when I try to write modified content to the specific xml file, something goes wrong. I guess I am missing
something.

here are some steps of my process:

I have schema and I generate java files.
I have content inside a xml file (depends on that schema)

Reading with "Factory.parse(xmlfile)" is not a problem (if I DON'T change that stuff)

here is my small schema:
--------------------------------------------------------
<?xml version="1.0"?>
<xs:schema
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://foo.com"
  xmlns:pb="http://foo.com"
  elementFormDefault="qualified">

<xs:element name="page">
 <xs:complexType>
    <xs:sequence>
       <xs:element name="modification" type="xs:dateTime"/>
       <xs:element name="type" type="xs:string"/>
       <xs:element name="header" type="xs:string" />
	   <xs:element name="container" type="pb:containerType" maxOccurs="unbounded"/>
   </xs:sequence>
  <xs:attribute use="required" name="id" type="xs:string" />
 </xs:complexType>
</xs:element>
 

<xs:complexType name="containerType">
 <xs:sequence>
    <xs:element name="modification" type="xs:dateTime"/>
    <xs:element name="type" type="xs:string"/>
    <xs:element name="contentblock"      type="pb:contentBlockType" maxOccurs="unbounded"/>
    <xs:element name="container"      type="pb:containerType" maxOccurs="unbounded"/>
 </xs:sequence>
<xs:attribute use="required" name="id" type="xs:string" />
</xs:complexType>

<xs:complexType name="contentBlockType">
 <xs:sequence>
    <xs:element name="modification" type="xs:dateTime"/>
    <xs:element name="text" type="xs:string"/>
    <xs:element name="type" type="xs:string"/>
    <xs:element name="headline" type="xs:string" minOccurs="0" maxOccurs="1" />
 </xs:sequence>
 <xs:attribute use="required" name="id" type="xs:string" />
</xs:complexType>
</xs:schema>
--------------------------------------------------------

and here is a very small XML file (based upon schema):
--------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<page xmlns="http://FOO.com" id="content1">
    <type>press</type>
    <header>HEADER</header>
    <modification>2005-04-27T09:30:10</modification>
    <container id="container1">
        <type>textcontainer</type>
        <modification>2005-04-19T09:30:10</modification>    
        <contentblock id="contentblock1">
            <type>textcontent</type>
            <text>fooooooo bar  BAR FOO</text>
            <modification>2005-05-30T09:30:10</modification>
        </contentblock>
    </container>
</page

--------------------------------------------------------


No I modify the stuff and after that I do:

XmlOptions xmlOptions = new XmlOptions();
            xmlOptions.setSavePrettyPrint();
            page.save(xmlFile, xmlOptions);


When I now read a second time my xml (the modified stuff) I got this message:
org.apache.xmlbeans.XmlException: C:\content1.xml:0: error: The document is not a page@http://foo.com: multiple document elements

I guess this is because of my (re-written) xml file has no root element <page>

my content1.xml looks now like:

<?xml version="1.0" encoding="UTF-8"?>
<xml-fragment id="content1">
  <foo:type xmlns:foo="http://foo.com">press</foo:type>
  <foo:header xmlns:foo="http://foo.com">hallo1</foo:header>
  <foo:modification xmlns:foo="http://foo.com">2005-06-08T16:57:23.672+02:00</foo:modification>
  <foo:container id="container1" xmlns:foo="http://foo.com">
    <foo:type>textcontainer</foo:type>
    <foo:modification>2005-04-19T09:30:10</foo:modification>
    <foo:contentblock id="contentblock1">
      <foo:type>textcontent</foo:type>
            <foo:text>hallo3</foo:text>
      <foo:modification>2005-06-08T16:57:23.672+02:00</foo:modification>
      <foo:headline>hallo2</foo:headline>
    </foo:contentblock>
  </foo:container>
</xml-fragment>


What am I doing wrong on using XmlBeans to *change* XML files?

Thanks for any advice.

-Matthias

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org


Re: problems on modifing XML files with XML_BEANS

Posted by Dave Harrison <da...@stacker.me.uk>.
Hi Matthias,

The problem here is you are saving the /contents/ out via the
PageDocument.Page type, rather than the PageDocument itself.

In 1.0.3 you can get round this two ways: either by calling save on the
document, or pass in XmlOptions.setSaveOuter().

An example never hurt (and I need the practice!). Try this:

import java.io.File;
import java.io.StringWriter;

import org.apache.xmlbeans.XmlOptions;

import com.foo.*;
public class Test {

public static void main(String args[]){
        PageDocument page = null;
        StringWriter out = new StringWriter();
       
        try{
            // Input your example doc:
            page = PageDocument.Factory.parse(new File("doc.xml"));
           
            // Change something (add another container):
            ContainerType container2 = page.getPage().addNewContainer();
            container2.set(page.getPage().getContainerArray(0));
           
            // Output
            XmlOptions xmlOptions = new XmlOptions();
            xmlOptions.setSavePrettyPrint();
            out.append("Oops:\n");
            page.getPage().save(out, xmlOptions);

            out.append("\n\nFixed:\n");
            page.getPage().save(out, xmlOptions);
          
            out.append("\n\nFixedAgain:\n");
            page.save(out, xmlOptions);
        } catch (Exception e){
            e.printStackTrace();
            return;
        }
        System.out.println(out.toString());
    }
}

Check out the javadoc for XmlObject where all is explained!
http://xmlbeans.apache.org/docs/1.0.4/reference/index.html

Hmm, seems 1.0.4's out and I didn't notice.

All the best,

Dave.


Matthias Wessendorf wrote:

>Hi,
>
>I am using XmlBeans for reading content of xml files. That works fine and all works like I expected.
>
>But when I try to write modified content to the specific xml file, something goes wrong. I guess I am missing
>something.
>
>here are some steps of my process:
>
>I have schema and I generate java files.
>I have content inside a xml file (depends on that schema)
>
>Reading with "Factory.parse(xmlfile)" is not a problem (if I DON'T change that stuff)
>
>here is my small schema:
>--------------------------------------------------------
><?xml version="1.0"?>
><xs:schema
>  xmlns:xs="http://www.w3.org/2001/XMLSchema"
>  targetNamespace="http://foo.com"
>  xmlns:pb="http://foo.com"
>  elementFormDefault="qualified">
>
><xs:element name="page">
> <xs:complexType>
>    <xs:sequence>
>       <xs:element name="modification" type="xs:dateTime"/>
>       <xs:element name="type" type="xs:string"/>
>       <xs:element name="header" type="xs:string" />
>	   <xs:element name="container" type="pb:containerType" maxOccurs="unbounded"/>
>   </xs:sequence>
>  <xs:attribute use="required" name="id" type="xs:string" />
> </xs:complexType>
></xs:element>
> 
>
><xs:complexType name="containerType">
> <xs:sequence>
>    <xs:element name="modification" type="xs:dateTime"/>
>    <xs:element name="type" type="xs:string"/>
>    <xs:element name="contentblock"      type="pb:contentBlockType" maxOccurs="unbounded"/>
>    <xs:element name="container"      type="pb:containerType" maxOccurs="unbounded"/>
> </xs:sequence>
><xs:attribute use="required" name="id" type="xs:string" />
></xs:complexType>
>
><xs:complexType name="contentBlockType">
> <xs:sequence>
>    <xs:element name="modification" type="xs:dateTime"/>
>    <xs:element name="text" type="xs:string"/>
>    <xs:element name="type" type="xs:string"/>
>    <xs:element name="headline" type="xs:string" minOccurs="0" maxOccurs="1" />
> </xs:sequence>
> <xs:attribute use="required" name="id" type="xs:string" />
></xs:complexType>
></xs:schema>
>--------------------------------------------------------
>
>and here is a very small XML file (based upon schema):
>--------------------------------------------------------
><?xml version="1.0" encoding="utf-8"?>
><page xmlns="http://FOO.com" id="content1">
>    <type>press</type>
>    <header>HEADER</header>
>    <modification>2005-04-27T09:30:10</modification>
>    <container id="container1">
>        <type>textcontainer</type>
>        <modification>2005-04-19T09:30:10</modification>    
>        <contentblock id="contentblock1">
>            <type>textcontent</type>
>            <text>fooooooo bar  BAR FOO</text>
>            <modification>2005-05-30T09:30:10</modification>
>        </contentblock>
>    </container>
></page
>
>--------------------------------------------------------
>
>
>No I modify the stuff and after that I do:
>
>XmlOptions xmlOptions = new XmlOptions();
>            xmlOptions.setSavePrettyPrint();
>            page.save(xmlFile, xmlOptions);
>
>
>When I now read a second time my xml (the modified stuff) I got this message:
>org.apache.xmlbeans.XmlException: C:\content1.xml:0: error: The document is not a page@http://foo.com: multiple document elements
>
>I guess this is because of my (re-written) xml file has no root element <page>
>
>my content1.xml looks now like:
>
><?xml version="1.0" encoding="UTF-8"?>
><xml-fragment id="content1">
>  <foo:type xmlns:foo="http://foo.com">press</foo:type>
>  <foo:header xmlns:foo="http://foo.com">hallo1</foo:header>
>  <foo:modification xmlns:foo="http://foo.com">2005-06-08T16:57:23.672+02:00</foo:modification>
>  <foo:container id="container1" xmlns:foo="http://foo.com">
>    <foo:type>textcontainer</foo:type>
>    <foo:modification>2005-04-19T09:30:10</foo:modification>
>    <foo:contentblock id="contentblock1">
>      <foo:type>textcontent</foo:type>
>            <foo:text>hallo3</foo:text>
>      <foo:modification>2005-06-08T16:57:23.672+02:00</foo:modification>
>      <foo:headline>hallo2</foo:headline>
>    </foo:contentblock>
>  </foo:container>
></xml-fragment>
>
>
>What am I doing wrong on using XmlBeans to *change* XML files?
>
>Thanks for any advice.
>
>-Matthias
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
>For additional commands, e-mail: user-help@xmlbeans.apache.org
>
>  
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org