Hi Duane,


Thanks for the prod in the right direction.



I solved it by creating a dummy document and filled it with new instances of 
the structure down to the element I wanted to save from the master document.  I 
then added that child element, and saved the dummy document to file.  Restoring 
was just like loading the master document.  A simple parse from the document 
root, and read down to the element I needed, prior to moving it back into the 
master document.



Regards



Andrew



________________________________
From: Duane Zamrok [mailto:zam...@cubrc.org]
Sent: 01 June 2010 15:16
To: user@xmlbeans.apache.org
Subject: RE: Replacing a document element with a fragment

I recently had a similar problem trying to parse fragment documents, and the 
response I got indicated that it is not possible. If the schema does not 
contain an element definition for the element you're trying to produce, then 
the document you're parsing and writing is 
invalid[http://www.w3.org/TR/2001/REC-xmlschema-0-20010502/#conformance]. 
However, if the schema does define your element then there is a corresponding 
XMLBean Document type that you can use to write and parse that document 
correctly. You might also consider some of the following for the case where you 
don't want to save the entire parent element and/or the contents of the parent 
are not the subject of a reference.

<element name="parent" type="tns:ParentType"/>
<complexType name="ParentType">
                <sequence>
                                <element name="childElement" 
type="tns:ChildType" maxOccurs="unbounded"/>
                </sequence>
</comlexType>

<element name="child" type="tns:ChildType"/>
<complexType name="ChildType">
                <sequence>
                                <element name="contents" type="any"/>
                </sequence>
</complexType>


// Generate my original parent document
ParentDocument doc = ParentDocument.Factory.newInstance();
ParentType parent = doc.addNewParent();
ChildType child = parent.addNewChildElement();

// Save the child element to the disk
ChildDocument childDoc = ChildDocument.Factory.newInstance();
childDoc.setChild(child);

FileWriter out = new FileWriter(new File("c:\\out.xml"));
out.write(doc.toString());

// Load the child and replace original
ChildDocument childDoc2 = ChildDocument.Factory.parse(new File("c:\\out.xml"));
parent.setChildElement(childDoc2.getChild());

Obviously when you're dealing with a list of elements this becomes more 
complicated, but you'll get the general idea I think.

Best Regards
-Duane


From: Danieli, Andrew [mailto:andrew.dani...@hp.com]
Sent: Tuesday, June 01, 2010 5:47 AM
To: user@xmlbeans.apache.org
Subject: Replacing a document element with a fragment

Hi,

Sorry to send this again, but I thought my accidental inclusion of RE: in the 
original subject line had prevented most people from looking at this.

I'm struggling with an xml fragment processing issue.  I'm using a fragment 
saved to a file as a backup of an existing portion of my main XML document.  
When the user chooses to restore this portion of main XML document, I just want 
to overwrite the current in memory <element> with the original held in the 
fragment.  The problem I get is after the factory parse, doc.setElement, then 
save to file of the main XML, there are duplicate tags in the file e.g
<parent>
   <element> -- original tags
       <element>  ----+ restored tags and contents
           <contents> |
       </element>    +
   </element>
</parent>

The document then causes validation errors when its reloaded, as its finding 
<element> a second time.

I've managed to find the right combination of settings in the XmlOptions to 
call element.save(options) and get the following:
<?xml version="1.0" encoding="UTF-8"?>
<element  xmlns="<mynamespace>" 
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance>
    <contents>
<element>

Note : No "xml-fragment" in the output, and I'm using default namespaces and 
prefixes to keep the XML clean.

I then try to read it in as

    Element restoredElement = Element.Factory.parse(fragmentFile, options)

and overwrite the original using

    parent.setElement(restoredElement);

This doesn't seem to have the desired affect, as its replacing element's 
contents and not element itself.

Any suggestions , or some example code that could handle a backup/restore of 
this type would be appreciated.

Regards

Andrew

Reply via email to