1

Currently, im having this for appending data to my items file:

    $xmldoc = new DOMDocument();
    $xmldoc->load('ex.xml');


    $item= $xmldoc->createElement('item');

    $item->setAttribute('id', '100');
    $item->setAttribute('category', 'Fitness');
    $item->setAttribute('name', 'Basketball');
    $item->setAttribute('url', 'http://google.com');
    $item->setAttribute('description', 'This is a description');
    $item->setAttribute('price', '899');


    $xmldoc->getElementsByTagName('items')->item(0)->appendChild($item);
    $xmldoc->save('ex.xml');

Now before appending this, I would like to check for an existing element "item" that has the same attribute id value.

And if it does it should update that element with these new data.

Currently it just appends and doesnt check anything.

3 Answers 3

2
$xmldoc = new DOMDocument();
$xmldoc->load('ex.xml');

$xpath = new DOMXPath($xmldoc);
$query = $xpath->query('/mainXML/items/item[@id = "100"]');

$create_new_node = false;
if($query->length == 0)
{
    $item = $xmldoc->createElement('item');
    $create_new_node = true;
}
else
{
    $item = $query->item(0);
}

$item->setAttribute('id', '100');
$item->setAttribute('category', 'Fitness');
$item->setAttribute('name', 'Basketball');
$item->setAttribute('url', 'http://google.com');
$item->setAttribute('description', 'This is a description');
$item->setAttribute('price', '899');

if($create_new_node)
{
    $xmldoc->getElementsByTagName('items')->item(0)->appendChild($item);
}
$xmldoc->save('ex.xml');
Sign up to request clarification or add additional context in comments.

5 Comments

Doesnt work, tried this and the query solution you just had - it just appends still
And it should be $xmldoc->getelementbyid, not $doc, but i fixed that still nothing
Still nothing, it just appends
@Karem: It works for me, so you'd better post your XML file.
@Karem: Fixed. I suggest you look into XPath, as it will help you a lot when dealing with XML.
1

I haven't used this functionality but looks like a good match for DOMDocument: Get Element By ID

If you get a matching element, edit it, and if not, post away.

1 Comment

getElementById will not work with xml unless it has a DTD which specifies which attributes are ID type
0

If you have a DTD for this xml file that specifies that the "id" attribute is an ID type (i.e. its value is unique in a document and uniquely identifies its element), then you can use DOMDocument::getElementById().

Most likely, however, you do not have a DTD. In this case, you should just use XPath:

$xmldoc = new DOMDocument();
$xmldoc->load('ex.xml');

$xpath = new DOMXPath($xmldoc);
$results = $xpath->query('//items/item[@id=100][0]');

if (!$results->length) {
    $item= $xmldoc->createElement('item');
    $item->setAttribute('id', '100');
    $item->setAttribute('category', 'Fitness');
    $item->setAttribute('name', 'Basketball');
    $item->setAttribute('url', 'http://google.com');
    $item->setAttribute('description', 'This is a description');
    $item->setAttribute('price', '899');

    $xmldoc->getElementsByTagName('items')->item(0)->appendChild($item);
    $xmldoc->save('ex.xml');
}

You should also consider using SimpleXML for this task. The way this xml is structured and manipulated would probably be better-suited to SimpleXML.

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.