Jump to content
php.lv forumi

SadDemon

Reģistrētie lietotāji
  • Posts

    52
  • Joined

  • Last visited

Posts posted by SadDemon

  1. saistībā ar šo visu, tika atrasts:

     

    $dom->preserveWhiteSpace = false; // izņemam tukšumus pirms ielādes.. parsēšanas
    $dom->formatOutput = true; // pie saglabāšanas, dati ir smuki sakārtoti

  2. jā, bet ja mērķis ir to visu informāciju pārrakstīt citā (jaunā) failā.. tikai sakārtotu.. vai tā vispār ir pieņemama lieta; vai tas būtu pavisam nepareizi.. nerunājot par to vai tam ir liela/maza nozīme..

  3. Nu iespējams pagaidām esmu kaut nedauds virzījies uz priekšu.. Tākā iepriekšējais label man ir pieejams iekš Flash, tad ir iespēja salīdzināt un pārrakstīt..

     

    *.php:

    <?php
    
    
    function domToArray($node, $level = 0) {
    
    $array = array();
    
    // if node has attributes
    if ($node->hasAttributes()) {
    	$attributes = $node->attributes;
    
    	if (!is_null($attributes)) {
    		foreach ($attributes as $attribute) {
    			$array[$attribute->name] = $attribute->value;
    
    			echo "[".$level."]\n".$array[$attribute->name]."<br>";
    
    			// if nodes attribute equals value, update attribute
    			if ($array[$attribute->name] == "----.jpg") {
    				$node->setAttribute("label", $array[$attribute->name]." [testējam]");
    
    				// save
    				global $dom;
    				global $xml_source;
    				$dom->save($xml_source);
    			}
    		}
    	}
    }
    
    // if node has childnodes
    if ($node->hasChildNodes()) {
    	$child = $node->childNodes;
    
    	for ($i = 0; $i < $child->length; $i++) {
    		$iChild = $child->item($i);
    		$array[$iChild->nodeName] = domToArray($iChild, $level+1);
    	}
    }
    }
    
    $xml_source = "tree_source.xml";
    $dom = new DomDocument();
    $dom->load($xml_source);
    
    domToArray($dom);
    
    ?>

  4. Izskaidrošu visu smalkāk:

     

    Gala produkts ir administrēšana galerijai iekš Flash.. kur ir tree komponents, kas saņem datus no *.xml:

    <node label="---- galerijas">
    <node label="---- (galerija)">
    	<node label="---- (kategorija)">
    		<node label="--.jpg"/>
    	</node>
    
    	<node label="---- (kategorija)">
    		<node label="----.jpg"/>
    		<node label="----.jpg"/>
    		<node label="----.jpg"/>
    	</node>
    </node>
    </node>

     

    ..un datus par attēliem saņem no attiecīga *.xml:

    <gallery title="----" thumbDir="-----" imageDir="----" random="false">
    <category name="----">
    	<image>
    		<date>----</date>
    		<title>----</title>
    		<desc>----</desc>
    		<thumb>----.jpg</thumb>
    		<img>----.jpg</img>
    	</image>
    </category>
    </gallery>

     

    Mainot informāciju, ar *.php nomainam konkrētās nodes label atribūtu pirmajā *.xml un informāciju otrajā *.xml..

     

    Te arī tā problēma, kad cenšos pirmajā *.xml failā piekļūt nodēm, ņemot galerijas nodes indexu.. galerijas nodes indexu un attēla nodes indexu..

  5. Balstoties uz v3rb0 iepriekš nopostēto skriptu, mazliet izpratu funkcijas principus un izveidoju līdzīgu funkciju, kas visu iekļauj iekš array().. tas manai acij draudzīgāks izskatās..

     

    *.php

    <?php
    
    
    function domToArray($node, $level = 0) {
    
    $array = array();
    
    // if node has attributes
    if ($node->hasAttributes()) {
    	$attributes = $node->attributes;
    
    	if (!is_null($attributes)) {
    		foreach ($attributes as $attribute) {
    			$array[$attribute->name] = $attribute->value;
    
    			echo "[".$level."]\n".$array[$attribute->name]."<br>";
    		}
    	}
    }
    
    // if node has childnodes
    if ($node->hasChildNodes()) {
    	$child = $node->childNodes;
    
    	for ($i = 0; $i < $child->length; $i++) {
    		$iChild = $child->item($i);
    		$array[$iChild->nodeName] = domToArray($iChild, $level+1);
    	}
    }
    }
    
    $xml_source = "tree_source.xml";
    $dom = new DomDocument();
    $dom->load($xml_source);
    
    domToArray($dom);
    
    ?>

     

     

    Jautājums ir - kā lai piekļūst kādam konkrētam mezglam?

  6. Pēc nervus bendējošiem mēģinājumiem, secinājums ir:

     

    # izmantojot getElementsByTagName(), visi pakārtotie mezgli nostājas vienā līmenī..

    # tātad, ja rakstam - $node = $dom->getElementsByTagName("node")->item(3)->getAttribute("label");

    ..iegūsim pirmo mezglu, kur label ir iekļauts "----.jpg".. u.t.t.

     

    Šajā gadījumā man tas nekādi neder..

     

     

    Palūgšu piemēru (netieku skaidrs)!

     

    Kā iegūt 4. līmeņa node label?

  7. <?php
    
    $xml_source = "tree_source.xml";
    
    $dom = new DomDocument();
    $dom->load($xml_source);
    
    $node = $dom->childNodes->item(0)->getAttribute("label");
    
    echo $node;
    
    ?>

     

    ..*.xml:

    <node label = "---- galerijas">
    <node label = "---- (galerija)">
    	<node label = "---- (kategorija)">
    		<node label = "----.jpg" />
    		<node label = "----.jpg" />
    		<node label = "----.jpg" />
    	</node>
    
    	<node label = "---- (kategorija)">
    		<node label = "----.jpg" />
    		<node label = "----.jpg" />
    		<node label = "----.jpg" />
    	</node>

     

    ..iegūstam pirmā mezgla label, bet kā tālak? ..tur arī seko tie error.

  8. Nju, ja daram: $node = $dom->childNodes[0]->childNodes[0]->getAttribute("label");

    ..tad seko "Fatal error: Cannot use object of type DOMNodeList as array"..

     

    bet.. ja: $node = $dom->childNodes->item(0)->childNodes->item(0)->getAttribute("label");

    ..tad tam seko "Fatal error: Call to undefined method DOMText::getAttribute()"!

     

    Nekādi neizprotu kāpēc.. (varbūt man kas piemirsies)..

  9. khmm.. ievietoju jautājumu klāt iepriekšējam, jo lieta atbilstoša..

     

    Problēma ir - nekādi neprotu piekļūt iekšējiem mezgliem.. izmantojam php dom funkcijas..

     

    *.xml faila izrāvums:

    <node label = "---- galerijas">
    <node label = "---- (galerija)">
    	<node label = "---- (kategorija)">
    		<node label = "----.jpg" />
    		<node label = "----.jpg" />
    		<node label = "----.jpg" />
    	</node>
    
    	<node label = "---- (kategorija)">
    		<node label = "----.jpg" />
    		<node label = "----.jpg" />
    		<node label = "----.jpg" />
    	</node>

    u.t.t..

     

    ..piemēram, kā iegūt iekšējā mezgla (kur "----.jpg") label atribūtu?

     

    Ja rodas jautājums - kāpēc ir tikai "node" mezgli un "lable" atribūti - tad atbilde ir - paredzēts Flash Tree komponentam..

     

    Jau iepriekš pateicos!

  10. Problēma - kārtojot masīvu pazūd dublikāta dati.. lūdzu kādu skaidrojumu, kāpēc tas tā?

     

    *.php faila saturs:

    <?php
    
    $doc = new DOMDocument;
    $doc -> load('test_input.xml');
    
    $content = array();
    $products = $doc -> getElementsByTagName('product');
    
    
    foreach ($products as $product) {
    $id = $product -> getAttribute('id');
    $category = $product -> getAttribute('category');
    
    $row = "product id: $id - category: $category";
    
    $content[$category] = $row;
    }
    
    ksort ($content);
    
    
    foreach ($content as $output) {
    echo $output."<br>";
    }
    
    ?>

     

     

    un *.xml faila saturs:

    <?xml version="1.0" encoding="utf-8"?>
    <root>
    <product id="1" category="a" />
    <product id="2" category="g" />
    <product id="3" category="d" />
    <product id="4" category="b" />
    <product id="5" category="a" />
    </root>

     

     

    Output:

    product id: 5 - category: a
    product id: 4 - category: b
    product id: 3 - category: d
    product id: 2 - category: g

     

    ..sekojoši, kur pazūd - <product id="1" category="a" /> - kā to novērst?

  11. *.xml saturs ir:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <Noliktava produkcijai="----">
    <product id="1" amount="2" name="test" color="test" article="-" category="citi" status="ir" catalog="test" page="-" price="-" />
    <product id="2" amount="7" name="vizītkaršu etvija" color="metāls" article="50042" category="birojam" status="ir" catalog="Reflect" page="87" price="2.64" />
    <product id="3" amount="25" name="vizītkaršu etvija" color="metāls" article="ap802301" category="birojam" status="ir" catalog="Anda Present" page="39" price="1.16" />
    <product id="4" amount="127" name="vizītkaršu etvija" color="metāls" article="1602270" category="birojam" status="ir" catalog="Marksmans (vecais)" page="-" price="2.19" />
    </Noliktava>

     

    un *.php saturs ir:

    <?php
    
    $amount_input = '2';
    $name_input = '';
    $category_input = '';
    $status_input ='';
    
    $doc = new DOMDocument;
    $doc -> load('../xml/depot.xml');
    
    $content = array();
    $products = $doc -> getElementsByTagName('product');
    
    foreach ($products as $product) {
    $id = $product -> getAttribute('id');
    $amount = $product -> getAttribute('amount');
    $name = $product -> getAttribute('name');
    $color = $product -> getAttribute('color');
    $article = $product -> getAttribute('article');
    $category = $product -> getAttribute('category');
    $status = $product -> getAttribute('status');
    
    if(ereg($amount_input, $amount)) {
    	$row = "<"."product id=\"".$id."\" amount=\"".$amount."\" name=\"".$name."\" color=\"".$color."\" article=\"".$article."\" category=\"".$category."\" status=\"".$status."\" />"."<br>";
    	$content[$id] = $row;	
    }
    }
    
    ksort($content);
    
    foreach ($content as $output) {
    echo $output;	
    }
    
    ?>

     

    Problēma Nr.1 - ja meklējamais ir "2" starp amount atribūtiem, tad atrodam.. "2", "25" un "127".. bet nepieciešams ir tikai "2" un "25".. tobiš, lai meklētu tikai no pirmā simbola..

     

    Problēma Nr.2 - vajadzētu panākt, lai.. ja $amount_input nav tukš un pārējie ir, tad "ereg($amount_input, $amount)"..

    ja $amount_input nav tukš un, piemēram, $category_input nav tukš.. tad "(ereg($amount_input, $amount) && ereg($category_input, $category))"..

     

     

    Iepriekš pateicos par palīdzību!

  12. Nu mans skripts tagad ir šāds:

     

    <?php
    
    $doc = new DOMDocument;
    $doc -> load('../xml/depot.xml');
    
    $content = array();
    $products = $doc -> getElementsByTagName('product');
    
    foreach ($products as $product) {
    $id = $product -> getAttribute('id');
    $amount = $product -> getAttribute('amount');
    $name = $product -> getAttribute('name');
    $color = $product -> getAttribute('color');
    $article = $product -> getAttribute('article');
    $category = $product -> getAttribute('category');
    $status = $product -> getAttribute('status');
    
    $row = "	<"."product id=\"".$id."\" amount=\"".$amount."\" name=\"".$name."\" color=\"".$color."\" article=\"".$article."\" category=\"".$category."\" status=\"".$status."\" />";
    
    $content[$id] = $row;	
    }
    
    ksort($content);
    
    
    $myXML = "../xml/temporary.xml";
    $ending = "</Noliktava>";
    
    $data = file_get_contents($myXML);
    $data = substr($data, 0, strpos($data, $ending)-1);
    
    foreach ($content as $output) {
    echo $output;
    
    $newData = "$output
    ";
    
    $data.=$newData;
    }
    
    
    $final_data = $data.
    $ending;
    
    $fHandle = fopen($myXML, "w+");
    fwrite($fHandle, $final_data);
    
    fclose($fHandle);
    
    ?>

     

    ..apjomi itkā ļoti lieli nebūs, bet varētu tuvāk paskaidrot - kas ir tā "maģiskā 8 Mb robeža"?

  13. Paldies "bubu"!

     

    Gan beigās vēl vajadzētu "</Noliktava>" tagu, tākā rakstīšana notiek iekš "foreach".. tur to likt nevar. Kā lai dabū dokumenta beigas, lai pievienotu aizveramo tagu?

     

    *.php saturs:

     

    <?php
    
    // ....
    
    
    $myXML = "../xml/temporary.xml";
    $ending = "</Noliktava>";
    
    $data = file_get_contents($myXML);
    $data = substr($data, 0, strpos($data, $ending)-1);
    
    foreach ($content as $output) {
    echo $output;
    
    $newData = "$output
    ";
    
    $data.=$newData;
    
    $fHandle = fopen($myXML, "w+");
    fwrite($fHandle, $data);
    }
    
    fclose($fHandle);
    
    ?>

×
×
  • Create New...