0

HIER IST cDATA FILE

Hey guys, I am trying to get only the text from the p tag inside of CDATA for description. I could render Cdata in my code component but the <p> tag is still inside of the text. I want to only render the first P tag Please help!

$(document).ready(function() {
    $.ajax({
      url: "https://somethignsomething.com",
      dataType:'xml',
      type:'GET',
        success: function(result) {
            
            var my_blog = $(result).find('channel item').first();
       
                console.log(result)
               // var my_link = my_blog.find('link').parent().attr('href');;
              //  var my_link = my_blog.find('link').attr('href');
                var my_title = my_blog.find('title').text();
               
                var my_description = my_blog.find('description').text();
               
                var img = my_blog.find('content\\:encoded, encoded').text();
                img = $.parseHTML(img);
                img = img[0].firstChild.src;
                var oldSrc = 'https://cdn11.net/m/resources/img/teaser/rht-full-blogteaser-medium-smartphones-290x268.jpg';
                var newSrc = img;
                $('img[src="' + oldSrc + '"]').attr('src', img);
            
                

            $('.text-content').replaceWith( 
                $('<h2 />',{
                    text: my_title
                }),
                $('<p />',{
                    text: my_description
                }),
                )           
      },
      error: function(error) {
        console.log(error);
      }
    }); 
});
2
  • It would help us if you show a small but complete, representative sample of XML you are trying to process together with the resulting HTML markup you want to extract/create from that sample. Commented Aug 31, 2021 at 5:35
  • @MartinHonnen hey dude sure i edited my quation with xml file image Commented Aug 31, 2021 at 7:12

1 Answer 1

1

If you feed the content of my_description to the HTML parser with e.g.

const htmlDoc = new DOMParser().parseFromString(my_description, 'text/html');

and select the first p element with

const p = htmlDoc.querySelector('p');

you have a p element DOM node.

I am not sure whether you want to insert that p element into another element (doable with target.appendChild(p) as a core DOM method) or read out its text with p.textContent and insert that somewhere.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much it works well :) i was thinking to solve problem with regex but i dont even need it with your solution thansk again :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.