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 7973427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:06:03+00:00 2026-06-04T08:06:03+00:00

I have find out how to replace specific node values with PHP string using

  • 0

I have find out how to replace specific node values with PHP string using PHP DOM, now I need print these XML values into form fields. For replacing I’m using this PHP code, and maybe somehow I can modify this code for printing specific values, because this XML file is very complex

$file = "../word/document.xml";                                            
    $fp = fopen($file, "rb") or die("error");
    $str = fread($fp, filesize($file));
    $xml = new DOMDocument();                                
    $xml->formatOutput = true;
    $xml->preserveWhiteSpace = false;
    $xml->loadXML($str) or die("Error");

    $root   = $xml->documentElement;
    $fnode  = $root->childNodes->item(0);

    $ori    = $fnode->childNodes->item(1);                      
    $ori1    = $ori->childNodes->item(3);
    $ori2   = $ori1->childNodes->item(1);
    $ori3   = $ori2->childNodes->item(1);
    $ori4   = $ori3->childNodes->item(1);
    $ori5   = $ori4->childNodes->item(1);

        $wt     = $xml->createElement("w:t");
    $wtText = $xml->createTextNode("".$name." ".$surname."");
    $wt->appendChild($wtText);
        $ori4->replaceChild($wt,$ori5);
        $xml->save("../word/document.xml");

I need to reach same value ($ori5) and print it to form field, but I’m not sure how to do that.

XML file can be found here:

<w:document mc:Ignorable="w14 wp14">
    <w:body>
        <w:tbl>
            <w:tr w:rsidR="000171B5" w:rsidTr="00272F6E">
                <w:tc>
                    <w:p w:rsidR="000171B5" w:rsidRPr="00075BEA" w:rsidRDefault="000171B5" w:rsidP="000171B5">
                        <w:r w:rsidRPr="00075BEA">
                            <w:t>Vardas pavarde</w:t>
                        </w:r>
                    </w:p>
                </w:tc>
            </w:tr>
        </w:tbl>
    </w:body>
</w:document>
  • 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-04T08:06:05+00:00Added an answer on June 4, 2026 at 8:06 am

    Sorry I was not able to get to this last night, but I was able to get a copy of your XML to work with. In the future, please only provide a sample of the XML you are using. Especially since the one you provided was unformatted. It took longer for me to format up to where I needed than it did for me to answer the question. And most people would just leave it rather than digging through all that. Here’s the formatted XML relating to the question after having been sampled.

    <w:document mc:Ignorable="w14 wp14">
        <w:body>
            <w:tbl>
                <w:tr w:rsidR="000171B5" w:rsidTr="00272F6E">
                    <w:tc>
                        <w:p w:rsidR="000171B5" w:rsidRPr="00075BEA" w:rsidRDefault="000171B5" w:rsidP="000171B5">
                            <w:r w:rsidRPr="00075BEA">
                                <w:t>Vardas pavarde</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                </w:tr>
            </w:tbl>
        </w:body>
    </w:document>
    

    Using the path you provided (item(0), item(1), etc…) I came up with the value Vardas pavarde. Which is in the W:t element above. The steps I took in the XML are below.

    0   /1  /3 /1 /1/1/1
    body/tbl/tr/tc/p/r/t = Vardas pavarde
    

    The easier way of going about getting this value is with XPATH. You can read up on it here if you have any more questions about it. This is a vastly covered subject so i’m just going to give you the XPATH I think you are looking for. If its not exactly what you are looking for you’ll have to tweak it, just use that link its pretty straight forward.

    $path = '//body/tbl/tr/tc/p/r/t';
    

    So, Add the following after you have loaded your DOMDocument.

    $xpath = new DOMXpath($xml);
    $path = '//body/tbl/tr/tc/p/r/t';
    $elements = $xpath->query($path);
    

    Since XPATH returns an array of results you’ll have to dump it to find the specific index you are looking for. But in this instance it should be $elements[1] since there is only one other element that matches that path before the one you are looking for. So…

    $ori5 = $elements[1];
    

    Also, you might want to consider using SimpleXML for reading XML. DOM is good if you are editing it, which you appear to be doing here, but if you need to read it, SimpleXML is a little more straight forward. Here’s a link to using XPATH with SimpleXML. Hope this helps!

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

Sidebar

Related Questions

I have a string and I need to find out whether it is a
I have a string and need to get a specific chunk of text out
I have a string and I need to find out what was matched in
How can you find out what transaction log backup files have been restored using
I have two Java instances of java.util.Date and I have to find out if
I am working on a project where I have to find out the keyword
I have been trying to find out why the following lines of code do
I have been trying to find out what the difference is between the IS
I want to find out which tables have been modified in the last hour
Have got an NSString *str = @12345.6789 and want to find out if there

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.