Giter VIP home page Giter VIP logo

nfxml's Introduction

nfXml

**Provides two simple to use functions to parse/stringify XML **

Project Manager: Marco Plaza

Project includes:

  • nfXmlRead.prg: standalone function, receives valid XML string or file name, returns VFP empty-based object.
  • nfXmlCreate.prg: receives VFP object , returns XML string.

Basic Usage

oVfp = nfXMLRead(cXMLstring | fileName [, cArraysList, cXpath,@cObjectMap ])
    && creates a vfp object from xml.

cXml = nfXmlCreate( oVfp )
    && creates XML from a VFP empty-based object

cXpaths = nfxPaths( cXml )
    && Parse XML w/o schema and show all
    && object paths - please check notes
    && below for important notes about
    && arrays in XML.

IMPORTANT NOTES

oVfp = nfXMLRead( cXMLstring , cArraysList, @cXpath)

pass xml string, returns a vfp object. ( empty based, requires vfp9 )

ARRAYS ( cArraysList )

Unlike Json, XML does not identify object arrays on the XML document itself, it does it on the XML Schema.

To gain simplicity and ease of use, nfXmlRead uses no XML Schema.

nfXmlRead identifies arrays by counting the child nodes with identical names; so any node with more than one child node with identical name is identified as array, as no object can contain more than 2 properties with the same name.

If you're sure your XML contains no arrays and any contained array will have at least more than one member, you don't need to worry.

For any other situation, you can pass a comma separated list of all the xpaths that must be treated as arrays: a excel workbook contains the following arrays:

"/Workbook/Styles/Style[],/Workbook/Worksheet[]/Table/Row[]/Cell[]"

NAME PROPERTIES WITH SPECIAL CHARACTERS OR SPACES

XML allows node names with spaces and special characters; nfXml safely converts them to valid vfp property names by replacing them on the following way:

  • ':' -> '_'
  • '(' -> '_l_'
  • ')' -> '_r_'
  • '-' -> '_h_'
  • '.' -> '_d_'

RESERVERD WORDS OR NON CHARACTER PROPERTY NAMES
will be prepended with '_' for example, you can't have an array called "row" since "row()" is a vfp function, then it gets renamed to _row also names starting with numbers and so on.

NODE ATTRIBUTES
node attributes are listed in a property called _attr_

NAMESPACES
Namespaces are prepended to node names, separated by "_" this way ss:Styles gets the vfp property name ss_styles;

object properties with "_" should be escaped with additional "_"

this way:

  • oxml.customer_id gets serialized as: <customer:id>

  • oxml.customer__id gets serialized as: <customer_id>

  • oxml.customer___id gets serialized as: <customer_:id>

OPTIONAL PARAMETERS
xpathExp: you can pass any xPath Expression for nfXMLRead to return only the desired node without parsing the entire document; useful for big xml files.

ERROR MANAGEMENT
program will throw error if you supply invalid xml; manage accordingly.

nfxml's People

Contributors

nfoxdev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

nfxml's Issues

Arrays missing values if the values don't follow each other in the XML

The following XML which results in an array only includes the last two values ...

<specification> <type>value 1</type> <type>value 2</type> <pitch>2</pitch> <type>value 3</type> <type>value 4</type> </specification>

Having looked it seems to rely on the previous node name being the same else when it hits the third tag it recreates the array.

About Reading-AppResponse

Hi, Marco.

If Spanish is your native language, please.

I am using your nfXML to read an XML and convert it to a VFP-object.

I am triyng to get info from the XML I attach (Colombian Electronic Invoicing), but I am having troubles.

I have written an script --attached too-- for showing you. And I put several MessageBoxes for we to see.
This script was based on info that I found in
https://www.tek-tips.com/viewthread.cfm?qid=1753479

We run the sample. If we suspend and go to the Debugger, we can see the image #1.

I am interested (in this question) in cac_DocumentResponse, and its only three sub-elements inside it:

|------------------------|----------------|-----------------|
|                        | Debugger shows | My script shows |
|------------------------|----------------|-----------------|
|cac_DocumentResponse    |                |                 |
|  cac_DocumentReference | object         | object          |
|  cac_Response          | array          | object          |
|  cac_LineResponse      | object         | object          |
|------------------------|----------------|-----------------|

I understand that I can get info from cac_DocumentReference and cac_Response directly as I show in image-04,....
But... I do not understand why the FOR EACH fails if the two FORs with AMEMBERS and GETPEM run both fine.

And also.... How can/should I walk thru the three sub-elements I mention before and thru the several cac:LineResponse's?

I show you two ways, but both fail. First inside the first FOR, and second commented inside #IF .F. and #ENDIF.
I use the method commented inside #IF .F. and #ENDIF, because the cac:LineResponse's can be several, non-defined how much.

I made some adjustments to nfXmlRead.prg as you can see marked as HCM, because nfXmlRead failed me in my scenarios.
Also I prefer LOCAL variables; nfXML.h inside nfXmlRead.prg is not mandatory; I used prefixes in mem vbles and parameters in order to prevent conflicts with vbles in our applications.
Please check them and try to adjust to a new version... where possible.

Thanks for sharing.

HERNAN CANO
Reading-AppResponse.zip

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.