簡體   English   中英

使用javascript從XML文件中提取文本的一部分

[英]Extract a part of a text from XML file with javascript

我正在嘗試從以下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: &apos;Stor&apos; / Amazon: &apos;Winnie the Pooh&apos;). 
                               **Part I want to delete =>** If this is the right ASIN for your product, update your data to match what&apos;s in the 
                                Amazon catalog. If it&apos;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: &apos;&apos; / Amazon: &apos;ST-30614&apos;). 
                               **Part I want to delete =>**If this is the right ASIN for your product, update your data to match what&apos;s in the 
                               Amazon catalog. If it&apos;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>

您是否嘗試過使用substringindexOf方法?

您的問題的示例:

var a = result.substring(34, result.indexOf("If"))

文檔: String.prototype.substring()-JavaScript | MDNString.prototype.indexOf()-JavaScript | MDN

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM