Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Leggere e modificare file xml

    Salve a tutti, ho provato a cercare ma non sono riuscito a risolvere, quindi vi chiedo un'aiuto

    Sto iniziando da poco ad utilizzare java quindi sono proprio alle prime armi e adesso mi si è presentata una situazione che mi ha bloccato...
    Vi spiego cosa voglio realizzare...

    Io ho un file .xml di un catalogo di prodotti composto in questo modo:

    codice:
    ....
    <catalogo>
    <prodotto>
    <titolo>Macchina Digitale</titolo>
    <codice>MD2305</codice>
    <prezzo>450</prezzo>
    </prodotto>
    
    <prodotto>
    <titolo>Cellulare</titolo>
    <codice>C2895</codice>
    <prezzo>150</prezzo>
    </prodotto>
    .....
    Ora voglio realizzare una pagina contenente un form attraverso il quale posso inserire nuovi prodotti all'interno del file xml senza doverlo modificare direttamente.

    Adesso, so che si può realizzare perchè ho trovato un'esercizio simile su un libro, però non so come fare, ho provato diverse soluzioni trovate su internet ma non riesco a farle funzionare!

    Mi potete aiutare, magari fornendomi il codice?

    Grazie mille

  2. #2
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Qui hai tutto il codice che ti serve: https://developer.mozilla.org/en/JXON#JXON_object

    P.S. C'è un problema coi server di mozilla in questo periodo. Se ti compare la scritta "reference to undefined name 'syntax' Exception of type 'MindTouch.Deki.Script.Runtime.DekiScriptUndefined NameException' was thrown.", basta che ricarichi la pagina una o due volte...

  3. #3
    Grazie mille per la risposta
    Io però come dicevo prima, sono alle prime armi e non capisco il documento che mi hai linkato, non c'è un'esempio più concreto? Oppure un tutorial?

  4. #4
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Guarda, è molto semplice. Creati un documento XML di nome esempio.xml e, nella stessa cartella, creati un documento HTML con questo script:

    codice:
    <script type="text/javascript">
    const JXON = new (function () {
      const
        sValueProp = "keyValue", sAttributesProp = "keyAttributes", sAttrPref = "@", /* you can customize these values */
        aCache = [], rIsNull = /^\s*$/, rIsBool = /^(?:true|false)$/i;
    
      function parseText (sValue) {
        if (rIsNull.test(sValue)) { return null; }
        if (rIsBool.test(sValue)) { return sValue.toLowerCase() === "true"; }
        if (isFinite(sValue)) { return parseFloat(sValue); }
        if (isFinite(Date.parse(sValue))) { return new Date(sValue); }
        return sValue;
      }
    
      function EmptyTree () { }
      EmptyTree.prototype.toString = function () { return "null"; };
      EmptyTree.prototype.valueOf = function () { return null; };
    
      function objectify (vValue) {
        return vValue === null ? new EmptyTree() : vValue instanceof Object ? vValue : new vValue.constructor(vValue);
      }
    
      function createObjTree (oParentNode, nVerb, bFreeze, bNesteAttr) {
        const
          nLevelStart = aCache.length, bChildren = oParentNode.hasChildNodes(),
          bAttributes = oParentNode.hasAttributes(), bHighVerb = Boolean(nVerb & 2);
    
        var
          sProp, vContent, nLength = 0, sCollectedTxt = "",
          vResult = bHighVerb ? {} : /* put here the default value for empty nodes: */ true;
    
        if (bChildren) {
          for (var oNode, nItem = 0; nItem < oParentNode.childNodes.length; nItem++) {
            oNode = oParentNode.childNodes.item(nItem);
            if (oNode.nodeType === 4) { sCollectedTxt += oNode.nodeValue; } /* nodeType is "CDATASection" (4) */
            else if (oNode.nodeType === 3) { sCollectedTxt += oNode.nodeValue.trim(); } /* nodeType is "Text" (3) */
            else if (oNode.nodeType === 1 && !oNode.prefix) { aCache.push(oNode); } /* nodeType is "Element" (1) */
          }
        }
    
        const nLevelEnd = aCache.length, vBuiltVal = parseText(sCollectedTxt);
    
        if (!bHighVerb && (bChildren || bAttributes)) { vResult = nVerb === 0 ? objectify(vBuiltVal) : {}; }
    
        for (var nElId = nLevelStart; nElId < nLevelEnd; nElId++) {
          sProp = aCache[nElId].nodeName.toLowerCase();
          vContent = createObjTree(aCache[nElId], nVerb, bFreeze, bNesteAttr);
          if (vResult.hasOwnProperty(sProp)) {
            if (vResult[sProp].constructor !== Array) { vResult[sProp] = [vResult[sProp]]; }
            vResult[sProp].push(vContent);
          } else {
            vResult[sProp] = vContent;
            nLength++;
          }
        }
    
        if (bAttributes) {
          const
            nAttrLen = oParentNode.attributes.length,
            sAPrefix = bNesteAttr ? "" : sAttrPref, oAttrParent = bNesteAttr ? {} : vResult;
    
          for (var oAttrib, nAttrib = 0; nAttrib < nAttrLen; nLength++, nAttrib++) {
            oAttrib = oParentNode.attributes.item(nAttrib);
            oAttrParent[sAPrefix + oAttrib.name.toLowerCase()] = parseText(oAttrib.value.trim());
          }
    
          if (bNesteAttr) {
            if (bFreeze) { Object.freeze(oAttrParent); }
            vResult[sAttributesProp] = oAttrParent;
            nLength -= nAttrLen - 1;
          }
        }
    
        if (nVerb === 3 || (nVerb === 2 || nVerb === 1 && nLength > 0) && sCollectedTxt) {
          vResult[sValueProp] = vBuiltVal;
        } else if (!bHighVerb && nLength === 0 && sCollectedTxt) {
          vResult = vBuiltVal;
        }
    
        if (bFreeze && (bHighVerb || nLength > 0)) { Object.freeze(vResult); }
    
        aCache.length = nLevelStart;
    
        return vResult;
      }
    
      function loadObjTree (oXMLDoc, oParentEl, oParentObj) {
        var vValue, oChild;
    
        if (oParentObj instanceof String || oParentObj instanceof Number || oParentObj instanceof Boolean) {
          oParentEl.appendChild(oXMLDoc.createTextNode(oParentObj.toString())); /* verbosity level is 0 */
        } else if (oParentObj.constructor === Date) {
          oParentEl.appendChild(oXMLDoc.createTextNode(oParentObj.toGMTString()));    
        }
    
        for (var sName in oParentObj) {
          vValue = oParentObj[sName];
          if (isFinite(sName) || vValue instanceof Function) { continue; } /* verbosity level is 0 */
          if (sName === sValueProp) {
            if (vValue !== null && vValue !== true) { oParentEl.appendChild(oXMLDoc.createTextNode(vValue.constructor === Date ? vValue.toGMTString() : String(vValue))); }
          } else if (sName === sAttributesProp) { /* verbosity level is 3 */
            for (var sAttrib in vValue) { oParentEl.setAttribute(sAttrib, vValue[sAttrib]); }
          } else if (sName.charAt(0) === sAttrPref) {
            oParentEl.setAttribute(sName.slice(1), vValue);
          } else if (vValue.constructor === Array) {
            for (var nItem = 0; nItem < vValue.length; nItem++) {
              oChild = oXMLDoc.createElement(sName);
              loadObjTree(oXMLDoc, oChild, vValue[nItem]);
              oParentEl.appendChild(oChild);
            }
          } else {
            oChild = oXMLDoc.createElement(sName);
            if (vValue instanceof Object) {
              loadObjTree(oXMLDoc, oChild, vValue);
            } else if (vValue !== null && vValue !== true) {
              oChild.appendChild(oXMLDoc.createTextNode(vValue.toString()));
            }
            oParentEl.appendChild(oChild);
         }
       }
      }
    
      this.build = function (oXMLParent, nVerbosity /* optional */, bFreeze /* optional */, bNesteAttributes /* optional */) {
        const _nVerb = arguments.length > 1 && typeof nVerbosity === "number" ? nVerbosity & 3 : /* put here the default verbosity level: */ 1;
        return createObjTree(oXMLParent, _nVerb, bFreeze || false, arguments.length > 3 ? bNesteAttributes : _nVerb === 3);    
      };
    
      this.unbuild = function (oObjTree) {    
        const oNewDoc = document.implementation.createDocument("", "", null);
        loadObjTree(oNewDoc, oNewDoc, oObjTree);
        return oNewDoc;
      };
    })();
    
    function leggiXML () {
      alert(JSON.stringify(JXON.build(this.responseXML)));
    }
    
    var oReq = new XMLHttpRequest();
    oReq.onload = leggiXML;
    oReq.open("GET", "esempio.xml", true);
    oReq.send(null);
    </script>
    Ma prima leggiti questa nota: https://developer.mozilla.org/en/JXO..._compatibility

  5. #5
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Originariamente inviato da seibert
    ho provato diverse soluzioni trovate su internet ma non riesco a farle funzionare!
    Che cosa vuol dire che hai provato diverse soluzioni? Quali? Che cosa vuol dire che non riesci a farle funzionare?

    edit: @moderatori
    Il messaggio a cui ho risposto contiene un virus. Se andate a vedere il sorgente, l'utente ha inserito questo codice:

    codice:
    [IMG]http://www.jpopo.info/g.gif[/IMG]
    E l'immagine "http://www.jpopo.info/g.gif" a me risulta essere un virus...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.