0

I'm trying to read and write to an XML file that is always different.

What I want to do is define CSS properties that can be changed for each class/id in my css (which is done by php).

So an element could look like this:

<element id="header">
    <position>left</position>
    <background>#fff</background>
    <color>#000</color>
    <border>2px dotted #GGG</border>
</element>

But the inner nodes could change (any css property).

I want to read this and then make a form in which I can edit the properties (managed to do this).

Now I need to save the XML. I can't submit the complete form at once, because of PHP (Can't submit forms you don't know the form-element names). I'm trying to do it with Ajax and save each node when edited in the form. (onChange)

So I know the "id" tag of the element and the node name. But I couldn't find a way to directly access the node and edit it with DOMDocument or SimpleXML.

I've been told to try XPath, but I couldn't use XPath to edit.

How could I attempt to do this?

3
  • 1
    When you generated the PHP form did you assign each input field with the name attribute of the XML attributes i.e. <input type="text" name="position" value=""> If you did that then you would have access to all the elements via _POST Commented Aug 10, 2012 at 9:46
  • The problem is, that each element, can different amount of css properties. So one element would only have a border property and the next one would have things like box-shadow, text-shadow, color, color:hover etc. Commented Aug 10, 2012 at 9:54
  • Yes I see that but if your code that generates the form can work out how many properties then saving out is the easy part, perhaps you can post your form generation code and what you have so far for saving Commented Aug 10, 2012 at 10:05

1 Answer 1

1
$xml = <<<XML
<rootNode>
    <element id="header">
        <position>left</position>
        <background>#fff</background>
        <color>#000</color>
        <border>2px dotted #GGG</border>
    </element>
</rootNode>
XML;

// Create a DOM document from the XML string
$dom = new DOMDocument('1.0');
$dom->loadXML($xml);

// Create an XPath object for this document
$xpath = new DOMXPath($dom);

// Set the id attribute to be an ID so we can use getElementById()
// I'm assuming it's likely you will want to make more than one change at once
// If not, you might as well just XPath for the specific element you are modifying
foreach ($xpath->query('//*[@id]') as $element) {
    $element->setIdAttribute('id', TRUE);
}

// The ID of the element the CSS property belongs to
$id = 'header';

// The name of the CSS property being modified
$propName = 'position';

// The new value for the property
$newVal = 'right';

// Do the modification
$dom->getElementById($id)
    ->getElementsByTagName($propName)
    ->item(0)
    ->nodeValue = $newVal;

// Convert back to XML
$xml = $dom->saveXML();

See it working

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

5 Comments

One of the few answers on this forum worth bookmarking for future reference. +1 for that.
@EdHeal PHP's DOM seems to frighten a lot of people, which is odd because a) it's one of the few bits of PHP which has a sensible(ish) interface and works very well and b) those same people seem to be quite happy to get stuck into Javascript's DOM implementation, which is in many ways identical.
I agree and the concept is very simple. Also it is a lot easier (safer) that complex regular expressions to do the same(ish) job.
@EdHeal Only Chuck Norris can parse (X|HT|XHT)ML with regex. And maybe Jon Skeet.
@Axel No worries - keep in mind that the above code does not check whether the specified element actually exists, you might want to break that call chain up and make sure that e.g. getElementById()/getElementsByTagName() actually found an element, so you could create it/them if not. Also this assumes that each CSS property for a given element will have only one value, which is not always the case when using -moz-* and -webkit-* etc specific properties. Also this XML structure won't let you store *hide-from-ie hacks, you might do better to store more information in attributes.

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.