参考 p5.XML

p5.XML

A class to describe an XML object.

Each p5.XML object provides an easy way to interact with XML data. Extensible Markup Language (XML) is a standard format for sending data between applications. Like HTML, the XML format is based on tags and attributes, as in <time units="s">1234</time>.

Note: Use loadXML() to load external XML files.

示例

方法

getParent

Returns the element's parent element as a new p5.XML object.

getName

Returns the element's name as a String.

An XML element's name is given by its tag. For example, the element JavaScript has the name language.

setName

Sets the element's tag name.

An XML element's name is given by its tag. For example, the element JavaScript has the name language.

The parameter, name, is the element's new name as a string. For example, calling myXML.setName('planet') will make the element's new tag name .

hasChildren

Returns true if the element has child elements and false if not.

listChildren

Returns an array with the names of the element's child elements as Strings.

getChildren

Returns an array with the element's child elements as new p5.XML objects.

The parameter, name, is optional. If a string is passed, as in myXML.getChildren('cat'), then the method will only return child elements with the tag .

getChild

Returns the first matching child element as a new p5.XML object.

The parameter, name, is optional. If a string is passed, as in myXML.getChild('cat'), then the first child element with the tag will be returned. If a number is passed, as in myXML.getChild(1), then the child element at that index will be returned.

addChild

Adds a new child element and returns a reference to it.

The parameter, child, is the p5.XML object to add as a child element. For example, calling myXML.addChild(otherXML) inserts otherXML as a child element of myXML.

removeChild

Removes the first matching child element.

The parameter, name, is the child element to remove. If a string is passed, as in myXML.removeChild('cat'), then the first child element with the tag will be removed. If a number is passed, as in myXML.removeChild(1), then the child element at that index will be removed.

getAttributeCount

Returns the number of attributes the element has.

listAttributes

Returns an Array with the names of the element's attributes.

Note: Use myXML.getString() or myXML.getNum() to return an attribute's value.

hasAttribute

Returns true if the element has a given attribute and false if not.

The parameter, name, is a string with the name of the attribute being checked.

Note: Use myXML.getString() or myXML.getNum() to return an attribute's value.

getNum

Return an attribute's value as a Number.

The first parameter, name, is a string with the name of the attribute being checked. For example, calling myXML.getNum('id') returns the element's id attribute as a number.

The second parameter, defaultValue, is optional. If a number is passed, as in myXML.getNum('id', -1), it will be returned if the attribute doesn't exist or can't be converted to a number.

Note: Use myXML.getString() or myXML.getNum() to return an attribute's value.

getString

Return an attribute's value as a string.

The first parameter, name, is a string with the name of the attribute being checked. For example, calling myXML.getString('color') returns the element's id attribute as a string.

The second parameter, defaultValue, is optional. If a string is passed, as in myXML.getString('color', 'deeppink'), it will be returned if the attribute doesn't exist.

Note: Use myXML.getString() or myXML.getNum() to return an attribute's value.

setAttribute

Sets an attribute to a given value.

The first parameter, name, is a string with the name of the attribute being set.

The second parameter, value, is the attribute's new value. For example, calling myXML.setAttribute('id', 123) sets the id attribute to the value 123.

getContent

Returns the element's content as a String.

The parameter, defaultValue, is optional. If a string is passed, as in myXML.getContent('???'), it will be returned if the element has no content.

setContent

Sets the element's content.

An element's content is the text between its tags. For example, the element JavaScript has the content JavaScript.

The parameter, content, is a string with the element's new content.

serialize

Returns the element as a String.

myXML.serialize() is useful for sending the element over the network or saving it to a file.

Notice any errors or typos? Please let us know. Please feel free to edit src/io/p5.XML.js and open a pull request!

相关参考