Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9217649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:40:47+00:00 2026-06-18T02:40:47+00:00

I have one input xml file. cat sample.xml <Text> &lt;p&gt;ABC &lt;/p&gt; </Text> R script

  • 0

I have one input xml file.

cat sample.xml

<Text>
    &lt;p&gt;ABC &lt;/p&gt;
</Text>

R script

library(XML)
doc = xmlTreeParse("sample.xml", useInternal = TRUE)
top<-xmlRoot(doc)

sub("&lt;","<",top[[1]])

How can i fix above pblm?

Error Message: Error in as.vector(x, “character”) :
cannot coerce type ‘externalptr’ to vector of type ‘character’

Edit: Aim is to use readHTMLTable() function for particular node in xml which has html table but it has xml markup( &gt; and &lt;) for > and < which need to be repalced first as readHTMLTable function cannot handle xml markup.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:40:48+00:00Added an answer on June 18, 2026 at 2:40 am

    If your question is to know how to replace a string in the content of an XML node, then you can check the following code, using the sample.xml file you provided :

    ## Parse the XML file
    doc <- xmlTreeParse("sample.xml", useInternal = TRUE)
    ## Select the nodes we want to update
    nodes <- getNodeSet(doc, "//Text")
    ## For each node, apply gsub on the content of the node
    lapply(nodes, function(n) {
      xmlValue(n) <- gsub("ABC","foobar",xmlValue(n))
    })
    

    Which will give you :

    R> doc
    <?xml version="1.0"?>
    <Text>
        &lt;p&gt;foobar &lt;/p&gt;
    </Text>
    

    Here you can see that “ABC” as been replaced by “foobar”.

    But, if you try this code with the substitution you want to achieve (replace “&lt;” wit “<“), it apparently won’t work :

    doc <- xmlTreeParse("sample.xml", useInternal = TRUE)
    nodes <- getNodeSet(doc, "//Text")
    lapply(nodes, function(n) {
      xmlValue(n) <- gsub("&lt;","<",xmlValue(n))
    })
    

    will give you :

    R> doc
    <?xml version="1.0"?>
    <Text>
        &lt;p&gt;ABC &lt;/p&gt;
    </Text>
    

    Why ? If you are working with XML files, you should know that some characters, mainly <, >, & and ” are reserved as they are part of the base XML syntax. As such, they cannot appear in the content of the nodes, otherwise parsing would fail. So they are replaced by entities, which are a sort of coding of these characters. For example, “<” is coded as “&lt;”, “&” is coded as “&amp;”, etc.

    So here, the content of your node contains a “<” character, which has been automatically converted to his entity “&lt;”. What you try to do with your code is to replace “&lt;” back with “<“, which R will gladly do for you, but as it is a text content of a node, the XML package will immediatly convert it back to “&lt;”.

    So, if what you want to achieve is to convert your string “&lt;p&gt;ABC &lt;/p&gt;” to a new XML node “<p>ABC </p>”, you can’t do it that way. A solution would be to parse your text string, detect the name and of the node (here, “p”) from it, create a new node with xmlNode(), give it the text content “ABC” and replace the string with the node you just created.

    Another quick and dirty way to do it would be first to replace all the entities in your file without parsing the XML. Something like this :

    txt <- readLines(file("sample.xml"))
    txt <- gsub("&lt;", "<", txt)
    txt <- gsub("&gt;", ">", txt)
    writeLines(txt, file("sample2.xml"))
    doc2 <- xmlTreeParse("sample2.xml", useInternal = TRUE)
    

    Which gives :

    R> doc2
    <?xml version="1.0"?>
    <Text>
      <p>ABC </p>
    </Text>
    

    But this is dangerous, because if there is a “real” “&lt;” entity in you file, parsing will fail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one input file which has html tag embedded in xml for example
I have one input field and one division with span children <input type=text id=input
Updated to be clear. Step One: I have a XML file that I want
I have a class that consume an XML file and produce text output based
I have a text read from a XML file stored in UTF8 encoding. C#
I have an input XML file which I need to copy 1:1 to the
i have one form which have some input box and some select box. i
I have a PL/pgSQL function which requires one input parameter which is the primary
I have one image submit button like this <input id=enter name=enter type=image value=Login src=images/btn_login.jpg/>
I have two arrays and I post the arrays in one input; <input name='sistem[]'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.