Just two quick questions

Discussion in 'XML' started by Horny Porno-thologist, Jan 27, 2004.

  1. Dear everyone,
    I managed to get most of my code (nesting documents etc.) worked out,
    but there are a few niggles.

    The first one is as follows:

    My first document is:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <WfMessage>
    <ATO_ATO>
    <ATO_FileLoadedIntoSystem>No</ATO_FileLoadedIntoSystem>
    <ATO_RawXML>XML DATA HERE</ATO_RawXML>
    </ATO_ATO>
    </ProcInstInputData>
    </ProcessTemplateExecute>
    </WfMessage>

    What I want to do is to insert my second XML file between the tags so that
    it reads:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <WfMessage>
    <ATO_ATO>
    <ATO_FileLoadedIntoSystem>No</ATO_FileLoadedIntoSystem>
    <ATO_RawXML>
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    (XML data in here...snipped for brevity)
    </ATO_RawXML>
    </ATO_ATO>
    </ProcInstInputData>
    </ProcessTemplateExecute>
    </WfMessage>

    (By the way, I do need the second root in the nested document!)
    I've come up with some code that comes up with the following XML:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <WfMessage>
    <ATO_ATO>
    <ATO_FileLoadedIntoSystem>No</ATO_FileLoadedIntoSystem>
    (data in here...snipped for brevity) // TAGS AND ROOT MISSING !!!
    </ATO_ATO>
    </ProcInstInputData>
    </ProcessTemplateExecute>
    </WfMessage>

    Which is nearly what I need when I use the following code:

    ATO_document = builder.parse( "D:\\ATO.xml" );
    XML_document = builder.parse( "D:\\XML.xml" );
    NodeList replacedNodeList = XML_document.getElementsByTagName("ATO_RawXML");
    Node foundnode = replacedNodeList.item(0);
    if ( foundnode != null )
    {
    Node modfiedReplacedDocument = replaceNode(XML_document, ATO_document,
    foundnode);}
    else
    {
    // error message here.
    }
    }
    and replaceNode is defined as follows:

    public static Node replaceNode(Document replacedDocument, Document
    replacingDocument, Node replacedNode)
    {
    //Create a documentFragment of the replacingDocument

    DocumentFragment docFrag = replacingDocument.createDocumentFragment();
    Element rootElement = replacingDocument.getDocumentElement();
    docFrag.appendChild(rootElement);

    //Import docFrag under the ownership of replacedDocument

    Node replacingNode =
    ((replacedDocument).importNode(docFrag, true));

    //In order to replace the node need to retrieve replacedNode's parent

    Node replaceNodeParent = replacedNode.getParentNode();
    replaceNodeParent.replaceChild(replacingNode, replacedNode);
    return replacedDocument;
    }

    I've tried using insertBefore functions in the replaced document to
    artificially recreate the <ATO_RawXML> tags, but the code
    grumbles that the parent element is missing. I'm sure a solution is very
    simple
    but I am losing my hair over this. Can anyone suggest anything (other than
    a lotion to keep my hair in) ?

    Best wishes
    Paul
    Horny Porno-thologist, Jan 27, 2004
    #1
    1. Advertising

  2. In article <401678e8$>,
    Horny Porno-thologist <> wrote:

    >What I want to do is to insert my second XML file between the tags so that
    >it reads:


    ><ATO_RawXML>
    > <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    > (XML data in here...snipped for brevity)
    ></ATO_RawXML>


    You can't do this. An xml declaration may only occur at the start of
    a document. There is no way to have it embedded inside another
    document.

    -- Richard
    --
    Spam filter: to mail me from a .com/.net site, put my surname in the headers.

    FreeBSD rules!
    Richard Tobin, Jan 27, 2004
    #2
    1. Advertising

  3. Horny Porno-thologist

    Paul Lee Guest

    Thanks Richard,

    Now all I have to do is solve the second problem - the tag being
    replaced. I can see why it is doing this, but I don't know how
    to circumvent the problem, unless it requires regenerating the
    "missing" tag.... can anyone suggest a very easy way of doing this?

    Best wishes

    Paul
    Paul Lee, Jan 27, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JS

    Two quick questions

    JS, Jan 18, 2006, in forum: Java
    Replies:
    7
    Views:
    345
    Roedy Green
    Jan 18, 2006
  2. Geoff Hague
    Replies:
    5
    Views:
    403
    Mark Parnell
    Jul 15, 2004
  3. Replies:
    1
    Views:
    343
    Richard Tobin
    Mar 13, 2005
  4. Elaine Jackson

    two quick questions

    Elaine Jackson, Aug 13, 2003, in forum: Python
    Replies:
    7
    Views:
    331
    Elaine Jackson
    Aug 13, 2003
  5. Michele Dondi

    Two quick questions about Perl6

    Michele Dondi, May 28, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    95
    Uri Guttman
    May 30, 2004
Loading...

Share This Page