我需要将一个大 XML 拆分为许多 child.xml 文件。除了更改值之外,我的代码可以正常工作。我需要插入我的字符串Titleproper
Bla bla text <num>X</num>
在现有元素中titleproper
.
I tried:
header.getElementsByTagName("titleproper").item(0).setTextContent(Titleproper);
但我的结果是:
<titleproper> Bla bla text lt;num;gt;1lt;/numgt;</titleproper>
我明白为什么,但我不知道如何欺骗这个限制。我需要在我的中插入 Text+Xml 代码titleproper
.
If the Titleproper
string 是 XML 本身,那么您需要解析此 XML 并将结果节点插入到目标树中。这org.w3c.dom.ls接口可以在这里帮助你。
// we need to get the DOMImplementation from the Document - if header is an
// Element then do:
DOMImplementationLS ls =
(DOMImplementationLS)header.getOwnerDocument().getImplementation();
// if header is already a Document then it's just
//DOMImplementationLS ls =
// (DOMImplementationLS)header.getImplementation();
// LSInput represents the source from which the XML to be parsed will be taken
LSInput input = ls.createLSInput();
input.setStringData(Titleproper);
// LSParser does the parsing
LSParser parser = ls.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
// parseWithContext(input, context, action) parses the fragment given by input
// and inserts the results at a position relative to the node "context". In this
// case we use ACTION_REPLACE_CHILDREN which means remove all the child nodes
// (if any) from the context node and replace them with the result of the parse.
// Alternative actions include replacing the context node entirely, inserting
// the parse results as siblings before or after the context node, etc.
parser.parseWithContext(input,
header.getElementsByTagName("titleproper").item(0),
LSParser.ACTION_REPLACE_CHILDREN);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)