Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   appending a new element to an existing xml file (http://www.velocityreviews.com/forums/t596245-appending-a-new-element-to-an-existing-xml-file.html)

ofuuzo1@yahoo.no 03-05-2008 01:23 PM

appending a new element to an existing xml file
 
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

ofuuzo1@yahoo.no 03-05-2008 01:34 PM

Re: appending a new element to an existing xml file
 
On 5 Mar, 14:23, ofuu...@yahoo.no 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>

Jürgen Kahrs 03-05-2008 01:56 PM

Re: appending a new element to an existing xml file
 
ofuuzo1@yahoo.no schrieb:
> On 5 Mar, 14:23, ofuu...@yahoo.no 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.

Martin Honnen 03-05-2008 02:19 PM

Re: appending a new element to an existing xml file
 
ofuuzo1@yahoo.no 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/

Malcolm Dew-Jones 03-05-2008 10:29 PM

Re: appending a new element to an existing xml file
 
ofuuzo1@yahoo.no wrote:
: On 5 Mar, 14:23, ofuu...@yahoo.no 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

Joseph Kesselman 03-05-2008 10:36 PM

Re: appending a new element to an existing xml file
 
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

Malcolm Dew-Jones 03-06-2008 06:24 PM

Re: appending a new element to an existing xml file
 
ofuuzo1@yahoo.no wrote:
: On 5 Mar, 14:23, ofuu...@yahoo.no 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.



ofuuzo1@yahoo.no 03-07-2008 11:29 AM

Re: appending a new element to an existing xml file
 
On 5 Mar, 14:56, Jürgen Kahrs <Juergen.KahrsDELETET...@vr-web.de>
wrote:
> ofuu...@yahoo.no schrieb:
>
>
>
> > On 5 Mar, 14:23, ofuu...@yahoo.no 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

ofuuzo1@yahoo.no 03-07-2008 11:31 AM

Re: appending a new element to an existing xml file
 
On 5 Mar, 15:19, Martin Honnen <mahotr...@yahoo.de> wrote:
> ofuu...@yahoo.no 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


All times are GMT. The time now is 07:04 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.