0

I am not so failiar with XML handling/managing, so I would like to ask your help in the following issue: I have an XML:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="somethiong_is_here...">
    <node1>value1</node1>
    <node2>
        <node21>
            <node211 enabled="true">
                <node2111>valuvavalue</node2111>
            </node211>
        </node21>
      </node2>
</root>

What I want, to change and/or replace the root node. It is fine for me, if I delete all attributes, namespace settings (all)...or create a new root node and add all children to the new node, where the result should be:

<?xml version="1.0" encoding="UTF-8"?>
    <root>
        <node1>value1</node1>
        <node2>
            <node21>
                <node211 enabled="true">
                    <node2111>valuvavalue</node2111>
                </node211 >
            </node21>
          </node2>
    </root>

As I mentioned, I am not so familiar with PHP - XML working. I tried it with SimpleXMLElement, DOMDocument, DOMElement, etc... But I wasn't able to delete root attributes or fully replace the root node.

I am using Php7.3

Thanks, Attila

5
  • Why don't you create a new document? What is your goal? I don't see a relevant difference between two versions. Commented Aug 19, 2022 at 10:10
  • The main goal would be to remove/delete the namesapce settigns from the root node, because all the xml content what I got, created by users - not generated - and unfortunately they make syntax mistakes. With the result XML I would be able to get values with xPath...but if the namespace setting has syntax error, the xPath not work :( Commented Aug 19, 2022 at 10:16
  • Does this answer your question? Remove namespace from XML using PHP Commented Aug 19, 2022 at 10:32
  • Unfortunaltey not :( Commented Aug 19, 2022 at 12:15
  • Here is no namespace definition affecting the elements in your example. Removing a namespace that is used by elements/attributes requires to completely recreate all nodes without namespaces, otherwise the namespace on the node will add the required namespace definition. Commented Aug 27, 2022 at 15:29

1 Answer 1

1

If you have the XML as a string

$xmlString = <<<'_XML'
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="somethiong_is_here...">
    <node1>value1</node1>
    <node2>
        <node21>
            <node211 enabled="true">
                <node2111>valuvavalue</node2111>
            </node211>
        </node21>
      </node2>
</root>
_XML;

you could remove all text within the root tag.

$noNamespace = preg_replace('/<root[^>]*>/', '<root>', $xmlString);

Then load the cleaned up XML as a SimpleXML object and process as used.

$xml = simplexml_load_string($noNamespace);
print_r($xml);

and looks like

SimpleXMLElement Object
(
    [node1] => value1
    [node2] => SimpleXMLElement Object
        (
            [node21] => SimpleXMLElement Object
                (
                    [node211] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [enabled] => true
                                )

                            [node2111] => valuvavalue
                        )

                )

        )

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

Comments

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.