cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

document to string

BrianJ
3-Visitor

document to string

Is it possible in java (with a mixture of ACL if needed) to convert a DOM document object to a string (without writing the document to a file and reading the file in)?

To a certain extent I can do this, but a couple of things are causing problems.

I want the string to have the xml declaration and (less important) the processing instruction Arbortext adds ( like ). And most importantly, I need the resulting string to have only user-specified attributes, no DTD/Schema provided defaults. And by attributes, I mean both true element attributes as well as "attributes" to Arbortext-specific processing instructions like _font and _touchup.

Any help would be greatly appreciated.
--

Brian Jensen
bjensen@bluelid.com
2 REPLIES 2
bibach
1-Newbie
(To:BrianJ)

Hi, Brian...

Given your requirements for somewhat finer control over the markup, you
might consider running the DOM document through an XSLT transform to get the
result you want. This would be mostly an identity transform, but would
allow you to control settings such as the presence of the XML declaration.

You'd also be able to override specific things, such as suppressing
attributes that correspond to their default values. It might be somewhat
tedious to generate the overrides for default attributes, depending on the
size of your DTD/Schema, though there are ways this could be automated, such
as an XSLT transform of an XML Schema or using ACL to access such
information in a DTD.

-Brandon 🙂

BrianJ
3-Visitor
(To:BrianJ)

I just found a way to do what I was looking for. It appears to do
everything I wanted and needed. Thanks to Brandon for his reply, which
I got just as I had verified my solution was working. Here's the code I
used, which will convert any (as far as I know) node, including a
Document object, into a string:

public static String toString(Node node) {
String oid = ((ANode) node).getFirstOID();
int flags = ARange.MARKUP_FORCE_PI | ARange.MARKUP_CHAR;
if(Acl.func("oid_type", oid).equals("9")) {
// XML comment
return Acl.func("oid_content", oid, "1");
} else {
Node nNode;
Document doc;
if(node.getNodeType() == Node.DOCUMENT_NODE) {
doc = (Document)node;
nNode = doc.getDocumentElement();
flags |= ARange.MARKUP_HEADER;
} else {
nNode = node;
doc = node.getOwnerDocument();
}

Range range = ((DocumentRange) doc).createRange();
range.setStartBefore(nNode);
range.setEndAfter(nNode);
return ((ARange) range).toMarkupStringEx(flags);
}
}

Thanks,
Brian



Brandon Ibach wrote:
> Hi, Brian...
>
> Given your requirements for somewhat finer control over the markup,
> you might consider running the DOM document through an XSLT transform
> to get the result you want. This would be mostly an identity
> transform, but would allow you to control settings such as the
> presence of the XML declaration.
>
> You'd also be able to override specific things, such as suppressing
> attributes that correspond to their default values. It might be
> somewhat tedious to generate the overrides for default attributes,
> depending on the size of your DTD/Schema, though there are ways this
> could be automated, such as an XSLT transform of an XML Schema or
> using ACL to access such information in a DTD.
>
> -Brandon 🙂
>
> On Thu, Feb 25, 2010 at 4:45 PM, Brian Jensen <bjensen@bluelid.com <br="/>> <>">mailto:bjensen@bluelid.com>> wrote:
>
> Is it possible in java (with a mixture of ACL if needed) to
> convert a DOM document object to a string (without writing the
> document to a file and reading the file in)?
>
> To a certain extent I can do this, but a couple of things are
> causing problems.
>
> I want the string to have the xml declaration and (less important)
> the processing instruction Arbortext adds ( like ). And most importantly, I need the
> resulting string to have only user-specified attributes, no
> DTD/Schema provided defaults. And by attributes, I mean both true
> element attributes as well as "attributes" to Arbortext-specific
> processing instructions like _font and _touchup.
>
> Any help would be greatly appreciated.
> --
>
>
>
> Brian Jensen
> bjensen@bluelid.com <">mailto:bjensen@bluelid.com>
>
>
Top Tags