Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit ff35aaa

Browse files
committed
add get/setContent() for XML
1 parent 7e84343 commit ff35aaa

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

core/src/processing/data/XML.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
*
4444
* @webref data:composite
4545
* @see PApplet#loadXML(String)
46+
* @see PApplet#parseXML(String)
47+
* @see PApplet#saveXML(XML, String)
4648
*/
4749
public class XML implements Serializable {
4850

@@ -75,16 +77,26 @@ protected XML() { }
7577
// this(parent.createReader(filename));
7678
// }
7779

80+
81+
/**
82+
* @param file description TBD
83+
*/
7884
public XML(File file) throws IOException, ParserConfigurationException, SAXException {
7985
this(file, null);
8086
}
8187

8288

89+
/**
90+
* @param options description TBD
91+
*/
8392
public XML(File file, String options) throws IOException, ParserConfigurationException, SAXException {
8493
this(PApplet.createReader(file), options);
8594
}
8695

8796

97+
/**
98+
* @param input description TBD
99+
*/
88100
public XML(InputStream input) throws IOException, ParserConfigurationException, SAXException {
89101
this(input, null);
90102
}
@@ -138,6 +150,9 @@ protected XML(Reader reader, String options) throws IOException, ParserConfigura
138150
}
139151

140152

153+
/**
154+
* @param name description TBD
155+
*/
141156
// TODO is there a more efficient way of doing this? wow.
142157
public XML(String name) throws ParserConfigurationException {
143158
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
@@ -749,6 +764,58 @@ public String getContent() {
749764
}
750765

751766

767+
public int getIntContent() {
768+
return getIntContent(0);
769+
}
770+
771+
772+
public int getIntContent(int defaultValue) {
773+
return PApplet.parseInt(node.getTextContent(), defaultValue);
774+
}
775+
776+
777+
public float getFloatContent() {
778+
return getFloatContent(0);
779+
}
780+
781+
782+
public float getFloatContent(float defaultValue) {
783+
return PApplet.parseFloat(node.getTextContent(), defaultValue);
784+
}
785+
786+
787+
public long getLongContent() {
788+
return getLongContent(0);
789+
}
790+
791+
792+
public long getLongContent(long defaultValue) {
793+
String c = node.getTextContent();
794+
if (c != null) {
795+
try {
796+
return Long.parseLong(c);
797+
} catch (NumberFormatException nfe) { }
798+
}
799+
return defaultValue;
800+
}
801+
802+
803+
public double getDoubleContent() {
804+
return getDoubleContent(0);
805+
}
806+
807+
808+
public double getDoubleContent(double defaultValue) {
809+
String c = node.getTextContent();
810+
if (c != null) {
811+
try {
812+
return Double.parseDouble(c);
813+
} catch (NumberFormatException nfe) { }
814+
}
815+
return defaultValue;
816+
}
817+
818+
752819
/**
753820
* @webref xml:method
754821
* @brief Sets the content of an element
@@ -758,6 +825,26 @@ public void setContent(String text) {
758825
}
759826

760827

828+
public void setIntContent(int value) {
829+
setContent(String.valueOf(value));
830+
}
831+
832+
833+
public void setFloatContent(float value) {
834+
setContent(String.valueOf(value));
835+
}
836+
837+
838+
public void setLongContent(long value) {
839+
setContent(String.valueOf(value));
840+
}
841+
842+
843+
public void setDoubleContent(double value) {
844+
setContent(String.valueOf(value));
845+
}
846+
847+
761848
/**
762849
* Format this XML data as a String.
763850
*

0 commit comments

Comments
 (0)