Home > Archive by category 'Xml-Php'
 

Set predefined value into xml path

index.php

1
2
3
4
5
6
7
8
9
<?php
   $xml = simplexml_load_file("new.xml");
   $book = $xml->xpath("/contact/personal[title='Manager']");
   echo $book[0]->title;
?>

new.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<contact id="111">
     <personal>
          <name>
               <first>J</first>
               <middle>J</middle>
               <last>J</last>
          </name>
          <title>Manager</title>
          <employer>National Company</employer>
          <dob>1951-02-02</dob>
     </personal>
</contact>

XML Path in action

index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
   $xml = simplexml_load_file("new.xml");
   $authors = $xml->xpath("/contact/personal/title");
   foreach($authors AS $author) {
      echo "$author<br />";
   }
?>

new.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<contact id="111">
     <personal>
          <name>
               <first>J</first>
               <middle>J</middle>
               <last>J</last>
          </name>
          <title>Manager</title>
          <employer>National Company</employer>
          <dob>1951-02-02</dob>
     </personal>
</contact>

Writing XML using PHP

This code will output the XML file in console as well as it will create a XML file in the name of ‘new.xml’ in the same directory of the PHP script.

index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
$employees = array();
$employees [] = array(
'name' => 'Devid',
'roll' => '34',
'marks' => "100"
);
$employees [] = array(
'name' => 'Claud',
'roll' => '20',
'marks' => "70"
);

$doc = new DOMDocument();
$doc->formatOutput = true;

$r = $doc->createElement( "students" );
$doc->appendChild( $r );

foreach( $employees as $employee )
{
$b = $doc->createElement( "student" );

$name = $doc->createElement( "name" );
$name->appendChild(
$doc->createTextNode( $employee['name'] )
);
$b->appendChild( $name );

$age = $doc->createElement( "roll" );
$age->appendChild(
$doc->createTextNode( $employee['roll'] )
);
$b->appendChild( $age );

$salary = $doc->createElement( "marks" );
$salary->appendChild(
$doc->createTextNode( $employee['marks'] )
);
$b->appendChild( $salary );

$r->appendChild( $b );
}

echo $doc->saveXML();
$doc->save("new.xml")
?>

Output

new.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0"?>
<students>
  <student>
    <name>Devid</name>
    <roll>34</roll>
    <marks>100</marks>
  </student>
  <student>
    <name>Claud</name>
    <roll>20</roll>
    <marks>70</marks>
  </student>
</students>

Reading an XML File using PHP

An explanation for read some data of a XML file with PHP.

Reading an XML File using PHP

test.xml

XML Code: file name – test.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="iso-8859-1"?>
<employees>
<employee>
<name>Mark</name>
<roll>27</roll>
<marks>50</marks>
</employee>
<employee>
<name>Jack</name>
<roll>25</roll>
<marks>40</marks>
</employee>
</employees>

index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
load( 'test.xml' );

$employees = $doc->getElementsByTagName( "employee" );
foreach( $employees as $employee )
{
$names = $employee->getElementsByTagName( "name" );
$name = $names->item(0)->nodeValue;

$rolls= $employee->getElementsByTagName( "roll" );
$roll= $rolls->item(0)->nodeValue;

$marks = $employee->getElementsByTagName( "marks" );
$marks = $marks->item(0)->nodeValue;

echo "<strong>$name</strong>";
echo "<strong>$roll</strong>";
echo "<strong>$marks</strong>";
}
?>

Output

1
2
3
4
5
6
7
8
9
10
11
Mark
27
50
Jack
25
40

 
 
 

Categories

Tag