我正在尝试从下面的XML文件中提取节点内文本的一部分。我设法删除之前的部分,但不能删除之后的部分。 我正在做文本比较,将要提取的部分作为边框。 但是,页面上没有任何显示。我在线搜索并使用了不同的方法,但都没有成功:
`var reg = new RegExp(/If/gi);`
`.replaceData( reg , "");`
或:
`y = y.replace(/If..../gi,"");`
XML:
<?xml version="1.0" encoding="UTF-8"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
<DocumentVersion>1.02</DocumentVersion>
<MerchantIdentifier></MerchantIdentifier>
</Header>
<MessageType>ProcessingReport</MessageType>
<Message>
<MessageID>1</MessageID>
<Result>
<MessageID>15</MessageID>
<ResultCode>Error</ResultCode>
<ResultMessageCode>8541</ResultMessageCode>
<ResultDescription>The SKU data .... Amazon catalog: brand (Merchant: 'Stor' / Amazon: 'Winnie the Pooh').
**Part I want to delete =>** If this is the right ASIN for your product, update your data to match what's in the
Amazon catalog. If it's not the right ASIN, make sure that your data for standard_product_id is
correct.
</ResultDescription>
<AdditionalInfo>
<SKU>30604</SKU>
</AdditionalInfo>
</Result>
<Result>
<MessageID>17</MessageID>
<ResultCode>Error</ResultCode>
<ResultMessageCode>8541</ResultMessageCode>
<ResultDescription>The SKU data .... Amazon catalog: part_number (Merchant: '' / Amazon: 'ST-30614').
**Part I want to delete =>**If this is the right ASIN for your product, update your data to match what's in the
Amazon catalog. If it's not the right ASIN, make sure that your data for
standard_product_id is correct.
</ResultDescription>
<AdditionalInfo>
<SKU>30614</SKU>
</AdditionalInfo>
</Result>
HTML和JavaScript:
<!DOCTYPE html>
<html>
<body>
<p id="demo2"></p>
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
//getting XML File
xhttp.open("GET", "/projectv1/raport", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
//Parse specific node
var result = xmlDoc.getElementsByTagName("ResultDescription")[0].childNodes[0];
//delete the first part of the text => The SKU data .... Amazon catalog:
var a = result.replaceData(0,220, "");
//trying to delete the second part with a text comparison because the part to extract is variable
var a = result.replace("This ......","");
var fin = document.getElementById("demo2");
fin.innerHTML = a;
}
</script>
</body>
</html>
答案 0 :(得分:0)
您是否尝试过使用substring
和indexOf
方法?
问题示例:
var a = result.substring(34, result.indexOf("If"))
文档: String.prototype.substring() - JavaScript | MDN,String.prototype.indexOf() - JavaScript | MDN