v*******8 发帖数: 20 | 1 how to insert a node into an existing xml file? - besides creating dom then
writing out to file?
the xml file is very long (several k lines), and I want to keep the comments
in original xml file. The node needs to be insert right before the end tag
of the root
thanks |
g*****g 发帖数: 34805 | 2 If it's simple one, using string and regex to locate the position
will be enough.
then
comments
tag
【在 v*******8 的大作中提到】 : how to insert a node into an existing xml file? - besides creating dom then : writing out to file? : the xml file is very long (several k lines), and I want to keep the comments : in original xml file. The node needs to be insert right before the end tag : of the root : thanks
|
S********a 发帖数: 1163 | 3 there are lots of ways to do it.
u can even use XSLT to do it.
then
comments
tag
【在 v*******8 的大作中提到】 : how to insert a node into an existing xml file? - besides creating dom then : writing out to file? : the xml file is very long (several k lines), and I want to keep the comments : in original xml file. The node needs to be insert right before the end tag : of the root : thanks
|
v*******8 发帖数: 20 | 4 can you share some more info? such as website/sample - I am using java and
the goal is not to re-generate the xml from dom (with which I cannot keep
the comments and maintain the exact same format)
Actually I was thinking about goodbug's way before my original post, but it
seems so "brute force" and low tech :) so I hesitated.
thanks
【在 S********a 的大作中提到】 : there are lots of ways to do it. : u can even use XSLT to do it. : : then : comments : tag
|
S********a 发帖数: 1163 | 5 using javax.xml.transform package, basically:
Transformer t = TransformerFactory.newTransformer(new StreamSource(xsltFile)
);
t.transform(new StreamSource(destFile), new StreamSource(sourceFile));
then u do whatever u want in xslt..
it
【在 v*******8 的大作中提到】 : can you share some more info? such as website/sample - I am using java and : the goal is not to re-generate the xml from dom (with which I cannot keep : the comments and maintain the exact same format) : Actually I was thinking about goodbug's way before my original post, but it : seems so "brute force" and low tech :) so I hesitated. : thanks
|
c*****t 发帖数: 1879 | 6 In your case, writing your own scanner would be highest performance and
straight forward than ANY other method.
Just keep track the last line that does not have an empty line (thus
guarrantees the root tag is in this line) and dump the rest.
Then search for the (root = root tag name), and insert your
tag in front.
then
comments
tag
【在 v*******8 的大作中提到】 : how to insert a node into an existing xml file? - besides creating dom then : writing out to file? : the xml file is very long (several k lines), and I want to keep the comments : in original xml file. The node needs to be insert right before the end tag : of the root : thanks
|
A**o 发帖数: 1550 | 7 only works if the file is well formatted.
【在 c*****t 的大作中提到】 : In your case, writing your own scanner would be highest performance and : straight forward than ANY other method. : Just keep track the last line that does not have an empty line (thus : guarrantees the root tag is in this line) and dump the rest. : Then search for the (root = root tag name), and insert your : tag in front. : : then : comments : tag
|
c*****t 发帖数: 1879 | 8 No need to. At most just track string...
【在 A**o 的大作中提到】 : only works if the file is well formatted.
|
t*******e 发帖数: 684 | 9 办法太多了,任何XML process的runtime都能干的。 |