0

I would like to remove all unnecessary nodes from a string

input file:

$data="<root>
  <president>
    <handle>0001</handle>
    <title>my title1</title>
    <body_html_>my description 1</body_html_>
    <vendor></vendor>
  </president>
  <president>
    <handle>0002</handle>
    <title>my title 2</title>
    <body_html_>my description 2</body_html_>
    <vendor></vendor>
  </president>
</root>";

I would like to remove all the nodes handle and vendor

i test

$doc = new SimpleXMLElement($data);
$segarr1 = $doc->President;
$segarr = $segarr1->title;

unset ($segarr[0]);


echo $doc->asXml();

delete only the first element

1 Answer 1

2

You need to iterate the children:

$doc = new SimpleXMLElement($data);

foreach($doc->children() as $segarr1) {
    unset($segarr1->title);
}

echo $doc->asXml();

If you have different children than president then you need to check:

if($segarr1->getName() == 'president') {
    unset($segarr1->title);
}
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.