读取 XML 属性文本并替换文本中间的一些单词
Read XML attribute text and replace some words in the middle of text
我正在尝试将 XML 节点属性值读取为字符串并替换检索到的文本中间的一些单词。但是到目前为止,我未能取得成功。让我详细说明我的问题。
我正在读取外部 XML 文件(使用 fs)并使用 dom 解析器读取内容。我正在尝试替换节点属性文本之一中包含的一些单词。我正在尝试做的示例如下。
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script type="text/javascript">
var parser, xmlDoc;
var text = "<bookstore><book>" +
"<title>test title</title>" +
"<author biography="Video provides a powerful way to help you prove your point.() When you click Online Video, you can paste in the embed code for the video you want to add. You can also type a keyword to search online for the video that best fits your document.() To make your document look professionally produced, Word provides header, footer, cover page, and text box designs that complement each other.()For example, you can add a matching cover page, header, and sidebar.">Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book></bookstore>";
parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");
var xy0 = xmlDoc.getElementsByTagName('author');
var xy1 = xy0.getAttribute('biography');
var xy2= xy1.replace("
","<br><br>");
console.log(xy2);
</script>
</body>
</html>
但是,这并没有按预期替换文本。是否有可能实现这一目标。如果不是,有哪些可能的选择?请善意地阐明这一点。提前致谢
我终于确定了问题所在。 JavaScript 不识别 "
"
它将其识别为新行。所以修复很简单,我只需要用 \n
替换 "
"
var xy2= xy1.replace("\n","<br>");
我正在尝试将 XML 节点属性值读取为字符串并替换检索到的文本中间的一些单词。但是到目前为止,我未能取得成功。让我详细说明我的问题。
我正在读取外部 XML 文件(使用 fs)并使用 dom 解析器读取内容。我正在尝试替换节点属性文本之一中包含的一些单词。我正在尝试做的示例如下。
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script type="text/javascript">
var parser, xmlDoc;
var text = "<bookstore><book>" +
"<title>test title</title>" +
"<author biography="Video provides a powerful way to help you prove your point.() When you click Online Video, you can paste in the embed code for the video you want to add. You can also type a keyword to search online for the video that best fits your document.() To make your document look professionally produced, Word provides header, footer, cover page, and text box designs that complement each other.()For example, you can add a matching cover page, header, and sidebar.">Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book></bookstore>";
parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");
var xy0 = xmlDoc.getElementsByTagName('author');
var xy1 = xy0.getAttribute('biography');
var xy2= xy1.replace("
","<br><br>");
console.log(xy2);
</script>
</body>
</html>
但是,这并没有按预期替换文本。是否有可能实现这一目标。如果不是,有哪些可能的选择?请善意地阐明这一点。提前致谢
我终于确定了问题所在。 JavaScript 不识别 "
"
它将其识别为新行。所以修复很简单,我只需要用 \n
"
"
var xy2= xy1.replace("\n","<br>");