Redian新闻
>
how to insert a node in existing xml
avatar
how to insert a node in existing xml# Java - 爪哇娇娃
v*8
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
avatar
g*g
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

avatar
S*a
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

avatar
v*8
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

avatar
S*a
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

avatar
c*t
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

avatar
A*o
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

avatar
c*t
8
No need to. At most just track
【在 A**o 的大作中提到】
: only works if the file is well formatted.
avatar
t*e
9
办法太多了,任何XML process的runtime都能干的。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。