appending a new element to an existing xml file

Discussion in 'XML' started by ofuuzo1@yahoo.no, Mar 5, 2008.

  1. Guest

    Hi,
    Is there anyway I can append a new element to an existing xml without
    first loading the existing file into a variable, adding the new
    element into the variable and saving it by overwriting the existing
    file name?

    Thanks
    Ofuuzo
     
    , Mar 5, 2008
    #1
    1. Advertising

  2. Guest

    On 5 Mar, 14:23, wrote:
    > Hi,
    > Is there anyway I can append a new element to an existing xml without
    > first loading the existing file into a variable, adding the new
    > element into the variable and saving it by overwriting the existing
    > file name?
    >
    > Thanks
    > Ofuuzo


    To smplify my question.
    I have nth xml files and I want to put all of them together in one xml
    file called new.xml. How can I do it?

    Example

    1.xml
    <head>
    <title>Test1</title>
    .....
    </head>

    2.xml
    <head>
    <title>Test2</title>
    .....
    </head>

    n.xml
    1.xml
    <head>
    <title>Test nth</title>
    .....
    </head>
     
    , Mar 5, 2008
    #2
    1. Advertising

  3. schrieb:
    > On 5 Mar, 14:23, wrote:
    >> Hi,
    >> Is there anyway I can append a new element to an existing xml without
    >> first loading the existing file into a variable, adding the new
    >> element into the variable and saving it by overwriting the existing
    >> file name?
    >>
    >> Thanks
    >> Ofuuzo

    >
    > To smplify my question.
    > I have nth xml files and I want to put all of them together in one xml
    > file called new.xml. How can I do it?
    >
    > Example
    >
    > 1.xml
    > <head>
    > <title>Test1</title>
    > ....
    > </head>
    >
    > 2.xml
    > <head>
    > <title>Test2</title>
    > ....
    > </head>
    >
    > n.xml
    > 1.xml
    > <head>
    > <title>Test nth</title>
    > ....
    > </head>


    Many others have asked this before.
    The official anwer is that you cant just
    concatenate files because XML data is allowed
    to have one root element only.

    Some parsers seem to tolerate XML files that
    consist of a sequence of XML root elements.
    But remember that these files are not well-
    formed XML files anymore, although the look so.
     
    Jürgen Kahrs, Mar 5, 2008
    #3
  4. wrote:

    > I have nth xml files and I want to put all of them together in one xml
    > file called new.xml. How can I do it?


    XSLT can do that. You will need to decide however on a new root element
    for the elements of those XML documents you want to combine into one
    document.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Mar 5, 2008
    #4
  5. wrote:
    : On 5 Mar, 14:23, wrote:
    : > Hi,
    : > Is there anyway I can append a new element to an existing xml without
    : > first loading the existing file into a variable, adding the new
    : > element into the variable and saving it by overwriting the existing
    : > file name?

    If your concern is the physical act of efficiently adding data to an
    existing file (such as what happens to log files) then I think the two
    ways to do this would be

    -1- Dont add the closing tag until you're finished adding data to the
    file. Simply append each xml-snippet as needed. If you need to manipulate
    the file before you're done then create a copy that has a closing tag.


    -2- Each time you prepend the next set of data you need to truncate the
    file slightly to remove the last line (containing the closing tag). If
    the closing tag is a constant then you know the length to truncate,
    otherwise you need to examine the last N bytes to find the location of the
    last tag, and truncate enough bytes to remove it before appending the new
    data.


    $0.10
     
    Malcolm Dew-Jones, Mar 5, 2008
    #5
  6. Jürgen Kahrs wrote:
    > Many others have asked this before.
    > The official anwer is that you cant just
    > concatenate files because XML data is allowed
    > to have one root element only.


    Possible kluge-around: External parsed entities can have multiple root
    elements. Pulling one of those into the body of a document with a single
    root element yields a well-formed XML document.


    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Mar 5, 2008
    #6
  7. wrote:
    : On 5 Mar, 14:23, wrote:
    : > Hi,
    : > Is there anyway I can append a new element to an existing xml without
    : > first loading the existing file into a variable, adding the new
    : > element into the variable and saving it by overwriting the existing
    : > file name?
    : >
    : > Thanks
    : > Ofuuzo

    : To smplify my question.
    : I have nth xml files and I want to put all of them together in one xml
    : file called new.xml. How can I do it?

    : Example

    : 1.xml
    : <head>
    : <title>Test1</title>
    : ....
    : </head>

    : 2.xml
    : <head>
    : <title>Test2</title>
    : ....
    : </head>

    : n.xml
    : 1.xml
    : <head>
    : <title>Test nth</title>
    : ....
    : </head>

    (my second answer, specific to this example).

    Wrap them all in an additional level of tagging.

    echo '<new-root>' > joined.xml
    cat 1.xml >> joined.xml
    cat 2.xml >> joined.xml
    ... etc.
    echo '</new-root>' >> joined.xml

    Now you have one big xml file with the root of "new-root" (assuming the
    files are valid, and ignoring issues of the <?xml version="1.0"....> being
    in there or not.
     
    Malcolm Dew-Jones, Mar 6, 2008
    #7
  8. Guest

    On 5 Mar, 14:56, Jürgen Kahrs <>
    wrote:
    > schrieb:
    >
    >
    >
    > > On 5 Mar, 14:23, wrote:
    > >> Hi,
    > >> Is there anyway I can append a new element to an existing xml without
    > >> first loading the existing file into a variable, adding the new
    > >> element into the variable and saving it by overwriting the existing
    > >> file name?

    >
    > >> Thanks
    > >>Ofuuzo

    >
    > > To smplify my question.
    > > I have nth xml files and I want to put all of them together in one xml
    > > file called new.xml. How can I do it?

    >
    > > Example

    >
    > > 1.xml
    > > <head>
    > > <title>Test1</title>
    > > ....
    > > </head>

    >
    > > 2.xml
    > > <head>
    > > <title>Test2</title>
    > > ....
    > > </head>

    >
    > > n.xml
    > > 1.xml
    > > <head>
    > > <title>Test nth</title>
    > > ....
    > > </head>

    >
    > Many others have asked this before.
    > The official anwer is that you cant just
    > concatenate files because XML data is allowed
    > to have one root element only.
    >
    > Some parsers seem to tolerate XML files that
    > consist of a sequence of XML root elements.
    > But remember that these files are not well-
    > formed XML files anymore, although the look so.


    Thanks. The new xml file does not need a new root element. All what I
    want is to transform each xml file and store all of them in a new xml
    file. I need help on how I can store the new.xml in a variable,
    concatenate "new.xml" and "nth_old.xml" and then store it back as
    "new.xml". This is what I have done:

    <?php

    for ($i = 0; $i < 50) {

    /* load the xml file and stylesheet as dom documents */
    $xsl = new DomDocument();
    $xsl->load("transform.xsl");

    $old = $i."old.xml";

    $inputdom = new DomDocument();
    $inputdom->load($old);

    /* create the processor and import the stylesheet */
    $proc = new XsltProcessor();
    $xsl = $proc->importStylesheet($xsl);

    /* transform and output the xml document */
    $newdom = $proc->transformToDoc($inputdom);

    print $newdom->saveXML();
    print $newdom->save("new.xml");
    }
    ?>

    Ofuuzo
     
    , Mar 7, 2008
    #8
  9. Guest

    On 5 Mar, 15:19, Martin Honnen <> wrote:
    > wrote:
    > > I have nth xml files and I want to put all of them together in one xml
    > > file called new.xml. How can I do it?

    >
    > XSLT can do that. You will need to decide however on a new root element
    > for the elements of those XML documents you want to combine into one
    > document.
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/


    Thanks. The new xml file does not need a new root element. All what I
    want is to transform each xml file and store all of them in a new xml
    file. I need help on how I can store the new.xml in a variable,
    concatenate "new.xml" and "nth_old.xml" and then store it back as
    "new.xml". This is what I have done:

    <?php

    for ($i = 0; $i < 50) {

    /* load the xml file and stylesheet as dom documents */
    $xsl = new DomDocument();
    $xsl->load("transform.xsl");

    $old = $i."old.xml";

    $inputdom = new DomDocument();
    $inputdom->load($old);

    /* create the processor and import the stylesheet */
    $proc = new XsltProcessor();
    $xsl = $proc->importStylesheet($xsl);

    /* transform and output the xml document */
    $newdom = $proc->transformToDoc($inputdom);

    print $newdom->saveXML();
    print $newdom->save("new.xml");
    }
    ?>

    Ofuuzo
     
    , Mar 7, 2008
    #9
    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. nrm
    Replies:
    3
    Views:
    535
  2. Frank
    Replies:
    1
    Views:
    353
    sloan
    Mar 6, 2007
  3. Anand
    Replies:
    4
    Views:
    415
    Fredrik Lundh
    Sep 26, 2007
  4. HANM
    Replies:
    2
    Views:
    723
    Joseph Kesselman
    Jan 29, 2008
  5. Konrad Hammerer
    Replies:
    14
    Views:
    614
    Jonathan N. Little
    Feb 7, 2008
Loading...

Share This Page