appending a new element to an existing xml file

O

ofuuzo1

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
 
O

ofuuzo1

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

Jürgen Kahrs

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.
 
M

Martin Honnen

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.
 
M

Malcolm Dew-Jones

(e-mail address removed) wrote:
: On 5 Mar, 14:23, (e-mail address removed) 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
 
J

Joseph Kesselman

Jürgen Kahrs said:
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.
 
M

Malcolm Dew-Jones

(e-mail address removed) wrote:
: On 5 Mar, 14:23, (e-mail address removed) 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.
 
O

ofuuzo1

(e-mail address removed) schrieb:





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
 
O

ofuuzo1

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.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top