XML DOM setTextContent [英] XML DOM setTextContent

查看:119
本文介绍了XML DOM setTextContent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个大XML拆分成多个child.xml文件。我的代码工作,除了一个变化值。我需要在现有的元素中插入我的字符串 Titleproper Bla bla text###/ num> code> titleproper 。

I need to split one big XML into many child.xml files. I my code works except for a change value. I need to insert my String Titleproper Bla bla text <num>X</num> in a existing element titleproper.

我试过:

header.getElementsByTagName("titleproper").item(0).setTextContent(Titleproper);

但我的结果是:

<titleproper> Bla bla text lt;num;gt;1lt;/numgt;</titleproper>

我明白为什么,但我不知道如何欺骗这个限制。我需要在我的 titleproper 中插入Text + Xml代码。

I understand why, but I don't know how to cheat this restriction. I need to insert Text+Xml code in my titleproper.

推荐答案

如果 Titleproper string是XML本身,那么您需要解析该XML并将生成的节点插入到目标树中。 org.w3c.dom.ls 界面可以帮助您。

If the Titleproper string is XML itself then you need to parse this XML and insert the resulting nodes into the target tree. The org.w3c.dom.ls interfaces can help you here.

// 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);

这篇关于XML DOM setTextContent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆