Php dom get child node by tag name

columns and columnDefs are redundant; that is, what you currently added to columnDefs shouls just go in your columns for the ones you want to have the tick marks. It should look like this:

/*Note that I'm assuming you're using DT 1.10.x, so the 'b' in front of boolean options
 *is unnecessary, if you aren't using 1.10.x then go ahead and put those back in.*/
 $(document).ready(function() {
    $('#log').dataTable( {
        "processing": true,
        "serverSide": true,
        "ajaxSource": "assetlog.php"
        "columns": [
           { "data": "id" },
           { "data": "assetcode" },
           { "data": "name"},
           { "data": "shift" },
           { "data": "datetime" },
           /*Note that data == true instead of ===, if you have 1 or 0 it isn't ===
           (strictly equal) to true but it is == (evaluates to equal) to true*/
           { "data": "stop_production",
             "render": function (data, type, row) {
                          return (data === true) ? '
                          ' : '';}
           },
           { "data": "furtheractions",
             "render": function (data, type, row) {
                          return (data == true) ? '
                          ' : '';}
           },
           { "data": "jobcomplete",
             "render": function (data, type, row) {
                          return (data == true) ? '
                          ' : '';}
           },
           { "data": "duration" }
       ]
    } );
} );

I made 3 changes to your code, 2 relevant to this issue and one to just update the syntax. The important 2 changes are:

  • Move the render function into each column that you want to have this behavior, instead of just having it in a redundant columnDefs
  • Change data === true to data == true since 1 is not === true but it is == true (=== is for strict comparisons taking type into account)

And the one less relevant change was:

  • bProcessing and bServerSide should be processing and serverSide . The former was the legacy format for DataTables options, which used hungarian notation, and since you don't have hungarian notation for columns you must be using v1.10.x which doesn't need that deprecated notation.

Note: You also mentioned that you get a blank page once you added the columns option, but you appear to be missing a comma after data: shift which could explain that.

PHP DOM: How to get child elements by tag name in an elegant manner

Questions : PHP DOM: How to get child elements by tag name in an elegant manner

2022-09-27T07:17:21+00:00 2022-09-27T07:17:21+00:00

802

I'm parsing some XML with PHP DOM extension anycodings_xml in order to store the data in some other anycodings_xml form. Quite unsurprisingly, when I parse an anycodings_xml element I pretty often need to obtain all anycodings_xml children elements of some name. There is the anycodings_xml method anycodings_xml DOMElement::getElementsByTagName($name), but anycodings_xml it returns all descendants with that name, anycodings_xml not just immediate children. There is also anycodings_xml the property DOMNode::$childNodes but (1) it anycodings_xml contains node list, not element list, and anycodings_xml even if I managed to turn the list items anycodings_xml into elements (2) I'd still need to check anycodings_xml all of them for the name. Is there really no anycodings_xml elegant solution to get only the children of anycodings_xml some specific name or am I missing something anycodings_xml in the documentation?

Some illustration:

loadXML(<<
  1
  2
  
    3
    4
  

EndOfXML
);

$bs = $document
    ->getElementsByTagName('a')
    ->item(0)
    ->getElementsByTagName('b');

foreach($bs as $b){
    echo $b->nodeValue . "\n";
}

// Returns:
//   1
//   2
//   3
//   4
// I'd like to obtain only:
//   1
//   2

?>

Total Answers 3

31

Answers 1 : of PHP DOM: How to get child elements by tag name in an elegant manner

simple iteration process

$parent = $p->parentNode;

foreach ( $parent->childNodes as $pp ) {

    if ( $pp->nodeName == 'p' ) {

        if ( strlen( $pp->nodeValue ) ) {
            echo "{$pp->nodeValue}\n";
        }

    }

}

0

2022-09-27T07:17:21+00:00 2022-09-27T07:17:21+00:00Answer Link

mRahman

5

Answers 2 : of PHP DOM: How to get child elements by tag name in an elegant manner

An elegant manner I can imagine would be anycodings_xml using a FilterIterator that is suitable anycodings_xml for the job. Exemplary one that is able anycodings_xml to work on such a said DOMNodeList and anycodings_xml (optionally) accepting a tagname to anycodings_xml filter for as an exemplary anycodings_xml DOMElementFilter from the Iterator anycodings_xml Garden does:

$a = $doc->getElementsByTagName('a')->item(0);

$bs = new DOMElementFilter($a->childNodes, 'b');

foreach($bs as $b){
    echo $b->nodeValue . "\n";
}

This will give the results you're anycodings_xml looking for:

1
2

You can find DOMElementFilter in the anycodings_xml Development branch now. It's perhaps anycodings_xml worth to allow * for any tagname as it's anycodings_xml possible with getElementsByTagName("*") anycodings_xml as well. But that's just some anycodings_xml commentary.

Hier is a working usage example online: anycodings_xml https://eval.in/57170

0

2022-09-27T07:17:21+00:00 2022-09-27T07:17:21+00:00Answer Link

raja

3

Answers 3 : of PHP DOM: How to get child elements by tag name in an elegant manner

My solution used in a production:

Finds a needle (node) in a haystack anycodings_xml (DOM)

function getAttachableNodeByAttributeName(\DOMElement $parent = null, string $elementTagName = null, string $attributeName = null, string $attributeValue = null)
{
    $returnNode = null;

    $needleDOMNode = $parent->getElementsByTagName($elementTagName);

    $length = $needleDOMNode->length;
    //traverse through each existing given node object
    for ($i = $length; --$i >= 0;) {

        $needle = $needleDOMNode->item($i);

        //only one DOM node and no attributes specified?
        if (!$attributeName && !$attributeValue && 1 === $length) return $needle;
        //multiple nodes and attributes are specified
        elseif ($attributeName && $attributeValue && $needle->getAttribute($attributeName) === $attributeValue) return $needle;
    }

    return $returnNode;
}

Usage:

$countryNode = getAttachableNodeByAttributeName($countriesNode, 'country', 'iso', 'NL');

Returns DOM element from parent anycodings_xml countries node by specified attribute anycodings_xml iso using country ISO code 'NL', anycodings_xml basically like a real search would do. anycodings_xml Find a certain country by it's name in anycodings_xml an array / object.

Another usage example:

$productNode = getAttachableNodeByAttributeName($products, 'partner-products');

Returns DOM node element containing only anycodings_xml single (root) node, without searching by anycodings_xml any attribute. Note: for this you must anycodings_xml make sure that root nodes are unique by anycodings_xml elements' tag name, e.g. anycodings_xml countries->country[ISO] - countries anycodings_xml node here is unique and parent to all anycodings_xml child nodes.

0

2022-09-27T07:17:21+00:00 2022-09-27T07:17:21+00:00Answer Link

joy