<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ca">
	<id>http://wiki.joanillo.org/index.php?action=history&amp;feed=atom&amp;title=XMLReader</id>
	<title>XMLReader - Historial de revisió</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.joanillo.org/index.php?action=history&amp;feed=atom&amp;title=XMLReader"/>
	<link rel="alternate" type="text/html" href="http://wiki.joanillo.org/index.php?title=XMLReader&amp;action=history"/>
	<updated>2026-08-30T14:36:28Z</updated>
	<subtitle>Historial de revisió per a aquesta pàgina del wiki</subtitle>
	<generator>MediaWiki 1.34.2</generator>
	<entry>
		<id>http://wiki.joanillo.org/index.php?title=XMLReader&amp;diff=2489&amp;oldid=prev</id>
		<title>Joan a 11:51, 21 abr 2009</title>
		<link rel="alternate" type="text/html" href="http://wiki.joanillo.org/index.php?title=XMLReader&amp;diff=2489&amp;oldid=prev"/>
		<updated>2009-04-21T11:51:28Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pàgina nova&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=Versió 1=&lt;br /&gt;
&lt;br /&gt;
És el cas mínim més senzill:&lt;br /&gt;
&lt;br /&gt;
Fitxer MyXMLFile_v1&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;company&amp;gt;&lt;br /&gt;
	&amp;lt;employee&amp;gt;&lt;br /&gt;
		&amp;lt;firstname&amp;gt;Tom&amp;lt;/firstname&amp;gt;&lt;br /&gt;
		&amp;lt;lastname&amp;gt;Cruise&amp;lt;/lastname&amp;gt;&lt;br /&gt;
	&amp;lt;/employee&amp;gt;&lt;br /&gt;
	&amp;lt;employee&amp;gt;&lt;br /&gt;
		&amp;lt;firstname&amp;gt;Paul&amp;lt;/firstname&amp;gt;&lt;br /&gt;
		&amp;lt;lastname&amp;gt;Enderson&amp;lt;/lastname&amp;gt;&lt;br /&gt;
	&amp;lt;/employee&amp;gt;&lt;br /&gt;
	&amp;lt;employee&amp;gt;&lt;br /&gt;
		&amp;lt;firstname&amp;gt;George&amp;lt;/firstname&amp;gt;&lt;br /&gt;
		&amp;lt;lastname&amp;gt;Bush&amp;lt;/lastname&amp;gt;&lt;br /&gt;
	&amp;lt;/employee&amp;gt;&lt;br /&gt;
&amp;lt;/company&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Fitxer XMLReader.java:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package v1;&lt;br /&gt;
&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import javax.xml.parsers.DocumentBuilder;&lt;br /&gt;
import javax.xml.parsers.DocumentBuilderFactory;&lt;br /&gt;
import org.w3c.dom.Document;&lt;br /&gt;
import org.w3c.dom.Element;&lt;br /&gt;
import org.w3c.dom.Node;&lt;br /&gt;
import org.w3c.dom.NodeList;&lt;br /&gt;
&lt;br /&gt;
public class XMLReader {&lt;br /&gt;
&lt;br /&gt;
 public static void main(String argv[]) {&lt;br /&gt;
&lt;br /&gt;
  try {&lt;br /&gt;
  File file = new File(&amp;quot;/home/joan/workspace/XMLReader/res/MyXMLFile_v1.xml&amp;quot;);&lt;br /&gt;
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();&lt;br /&gt;
  DocumentBuilder db = dbf.newDocumentBuilder();&lt;br /&gt;
  Document doc = db.parse(file);&lt;br /&gt;
  doc.getDocumentElement().normalize();&lt;br /&gt;
  System.out.println(&amp;quot;Root element &amp;quot; + doc.getDocumentElement().getNodeName());&lt;br /&gt;
  NodeList nodeLst = doc.getElementsByTagName(&amp;quot;employee&amp;quot;);&lt;br /&gt;
  System.out.println(&amp;quot;Information of all employees&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
  for (int s = 0; s &amp;lt; nodeLst.getLength(); s++) {&lt;br /&gt;
&lt;br /&gt;
    Node fstNode = nodeLst.item(s);&lt;br /&gt;
    &lt;br /&gt;
    if (fstNode.getNodeType() == Node.ELEMENT_NODE) {&lt;br /&gt;
  &lt;br /&gt;
           Element fstElmnt = (Element) fstNode;&lt;br /&gt;
      NodeList fstNmElmntLst = fstElmnt.getElementsByTagName(&amp;quot;firstname&amp;quot;);&lt;br /&gt;
      Element fstNmElmnt = (Element) fstNmElmntLst.item(0);&lt;br /&gt;
      NodeList fstNm = fstNmElmnt.getChildNodes();&lt;br /&gt;
      System.out.println(&amp;quot;First Name : &amp;quot;  + ((Node) fstNm.item(0)).getNodeValue());&lt;br /&gt;
      NodeList lstNmElmntLst = fstElmnt.getElementsByTagName(&amp;quot;lastname&amp;quot;);&lt;br /&gt;
      Element lstNmElmnt = (Element) lstNmElmntLst.item(0);&lt;br /&gt;
      NodeList lstNm = lstNmElmnt.getChildNodes();&lt;br /&gt;
      System.out.println(&amp;quot;Last Name : &amp;quot; + ((Node) lstNm.item(0)).getNodeValue());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
  } catch (Exception e) {&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Versió 3 i 4=&lt;br /&gt;
La v3 és un codi extret de http://www.informit.com/library/content.aspx?b=STY_XML_21days&amp;amp;seqNum=207 que fa precisament el que jo vull: llegeix un fitxer XML, amb elements que tenen atributs, i elements que tenen fills. A més de llegir-parsejar el fitxer, el torna a escriure. El codi original espera trobar un argument que li passem en línia de comandes. Amb el Eclipse, això es fa amb Run &amp;gt; Run Configurations &amp;gt; Argument. Tanmateix, jo modifico el codi per tal d'obrir directament el fitxer XML:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
document = builder.parse(&amp;quot;res/ch16_01.xml&amp;quot;);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
La v4 és el mateix que la v3, però ja utilitzo un fitxer amb llenguatge ''puppetml''.&lt;br /&gt;
&lt;br /&gt;
{{Autor}}, abril 2009&lt;br /&gt;
[[Llenguatge Java]]&lt;/div&gt;</summary>
		<author><name>Joan</name></author>
		
	</entry>
</feed>