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

  • SEARCH
  • Home
  • 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 8776167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:59:11+00:00 2026-06-13T18:59:11+00:00

I am reading a xml file, and I add some additional text, but I

  • 0

I am reading a xml file, and I add some additional text, but I can’t get exact text because some special characters automatically converted.

I try this:

<book>
  <book-meta>
    <book-id pub-id-type="doi">1545</book-id>
    <book-title>Regenerating <?tex?> the Curriculum</book-title>
  </book-meta>
</book>

Script:

use strict;

use XML::Twig;

open(my $out, '>', 'Output.xml') or die "can't Create stroy file $!\n";

my $story_file = XML::Twig->new(
    twig_handlers => {
        'book-id'       => sub { $_->set_text('<?sample?>') },
        keep_atts_order => 1,
    },
    pretty_print => 'indented',
);

$story_file->parsefile('sample.xml');
$story_file->print($out);

Output:

<book>
  <book-meta>
    <book-id pub-id-type="doi">&lt;?sample?></book-id>
      <book-title>Regenerating <?tex?> the Curriculum</book-title>
  </book-meta>
</book>

I would like output as:

<book>
      <book-meta>
        <book-id pub-id-type="doi"><?sample?></book-id>
          <book-title>Regenerating <?tex?> the Curriculum</book-title>
      </book-meta>
    </book>

How can I escape this type of character in XML twig. I tried the set_asis option, but I can’t get it to work.

  • 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-13T18:59:12+00:00Added an answer on June 13, 2026 at 6:59 pm

    XML::Twig is correctly inserting the string <?sample?> for you as you are asking for a PCDATA node to be added and < must be replaced with &lt; in such a node. However what you want is a processing instruction node.

    The easiest way to insert such a node using XML::Twig is using the set_inner_xml method, which will parse an XML tree fragment from a string and insert it as the contents of the current node.

    If you replace

    $_->set_text('<?sample?>')
    

    with

    $_->set_inner_xml('<?sample?>')
    

    then your code should do what you want. The output I get is

    <book>
      <book-meta>
        <book-id pub-id-type="doi"><?sample?></book-id>
        <book-title>Regenerating <?tex?> the Curriculum</book-title>
      </book-meta>
    </book>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add some data to a xml file like: <people> <person name=Jo>
What I am doing is reading a xml file and trying to add a
When reading a XML file with linq to XML using a XDocument and there
I am reading an xml file using javascript and then I need to submit
I am reading a xml file with: $reader = new XMLReader(); $reader->xml($myXml, NULL, LIBXML_NOWARNIG
I'm reading an xml file using Jaxb, now the contents of the xml file
While implementing XML file reading/writing in my application I saw that when I call
I'm using Qt C++ and am reading in an XML file for data. I
how do i ignore exceptions in c#? i am reading a remote xml file,
I planning on reading profile data from a xml file in JSP. Now i

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.