Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML to Array???

Status
Not open for further replies.

redpotato

Programmer
Feb 15, 2010
24
CA
I was trying to reach the deepest element in a xml.
for example,

<a>
<b></b>
</a>
<a>
<b>
<c></c>
</b>
</a>
<a>
<b>
<c>
<d></d>
</c>
</b>
</a>

in that kind of structure of xml, how can I reach <d></d> element?
could someone explain? (someone mentioned, it needs to use simpleXML but I still have no idea)
 
depends on what you want to do with the element. Did you want to change it? if so, something like this for starters may work:

Code:
$xmldoc = 'file.xml';

$newvalue = 'some_new_value';

$doc = new DOMDocument;
$doc->formatOutput = true;
$doc->preserveWhiteSpace = false;
$doc->load("$xmldoc");
$text = $doc->createTextNode($newvalue);

$value = $doc->getElementsByTagName('d')->item(0);
$value->firstChild->nodeValue = $newsvalue;

$doc->save($xmldoc);

If you just want to display it, you might try:

Code:
$xmldoc = 'file.xml';

$xmlDoc = new DOMDocument();
$xmlDoc->load("$xmldoc");

function startTag($parser, $data){
    echo "<p>";
}

function endTag($parser, $data){
    echo "</p><br>";
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($xmldoc, "r");
$data = fread($fp, 80000);
if(!(xml_parse($xml_parser, $data, feof($fp)))){
    die("Error on line " . xml_get_current_line_number($xml_parser));
}

Regards,
Chuck
 
Sorry, Chuck.

If I run your code, it comes with Warning: xml_parse() [function.xml-parse]: Unable to call handler contents() in G:\xampp\htdocs\xml_s.php

any idea?
 
just realized that, I don't think it is for getting the value where is the deepest element.
 
What is the specificity of d? Which generic factor(s) determining it? Is the name d generic or just figurative? How do you want to do with node-d? Have to more specific.
 
that was just an example.
as I mentioned, I need to get a value which sits the deepest element in XML. doesn't matter where the element is. doesn't matter d or c. just try to find the value of the deepest element in XML
 
can you explain the purpose of this requirement? a real world example of the usefulness of this? that might help us find a solution.
 
Now that I know how critical the question is to op's work and how "much" "driving will" to get help op have, I can all the same trying to help. How the script actually help will be a test to op's readiness that I will not involve.
[tt]
//your givens
[green]$xfile="data.xml";[/green]

$xdoc=simplexml_load_file($xfile);
$nlist=$xdoc->xpath("//*[count(child::*)=0]");

$maxdepth=0;
$nmaxlist=array(); //this will hold the desired results

foreach ($nlist as $node) {
$depth=count($node->xpath(".//ancestor::*"));
if ($depth>$maxdepth) {
$maxdepth=$depth;
$nmaxlist=array();
array_push($nmaxlist, $node);
} elseif ($depth=$maxdepth) {
array_push($nmaxlist, $node);
}
}

foreach ($nmaxlist as $node) {
echo $node->getName()." : ".$node."\n";
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top