Skip to content

Commit 95fcdb2

Browse files
committed
Added another overloaded static method to XML class:
static JSONObject toJSONObject(Reader reader, JSONPointer path, JSONObject replacement) Added testMethodTwo() in XMLTest_SWE262 test. Added Catalog.xml for testing purposes. Updated methods to handle tag attributes.
1 parent ffc1e32 commit 95fcdb2

3 files changed

Lines changed: 271 additions & 6 deletions

File tree

src/main/java/org/json/XML.java

Lines changed: 129 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,9 @@ public static JSONObject toJSONObject(Reader reader, JSONPointer path)
730730
containsNumber = i;
731731
}
732732
}
733-
System.out.println("Contains number: " + containsNumber);
733+
734+
System.out.println("Contains number: " + containsNumber);
735+
734736
String parseTag;// = (containsNumber == 0) ? pathArray[pathSize - 1] : pathArray[containsNumber - 1];
735737
if(containsNumber == 0)
736738
{
@@ -765,16 +767,16 @@ public static JSONObject toJSONObject(Reader reader, JSONPointer path)
765767

766768
String currTag ="";
767769

768-
String startString = "<" + parseTag + ">";
769-
String exitString = "</" + parseTag + ">";
770+
String startString = "<" + parseTag;
771+
String exitString = "</" + parseTag;
770772

771773
while (x.more()) {
772774
x.skipPast("<");
773775
currTag = "<" + x.nextContent();
774776
//System.out.println(currTag);
775777

776778
//Start recording after start string reached.
777-
if(currTag.equalsIgnoreCase(startString))
779+
if(currTag.contains(startString))
778780
cycle = true;
779781

780782

@@ -785,7 +787,7 @@ public static JSONObject toJSONObject(Reader reader, JSONPointer path)
785787
}
786788

787789
//Start recording after start string reached.
788-
if(currTag.equalsIgnoreCase(exitString))
790+
if(currTag.contains(exitString))
789791
{
790792
if(containsNumber != 0)
791793
cycle = false;
@@ -813,7 +815,128 @@ public static JSONObject toJSONObject(Reader reader, JSONPointer path)
813815

814816
public static JSONObject toJSONObject(Reader reader, JSONPointer path, JSONObject replacement)
815817
{
816-
return null;
818+
//Logic
819+
//Get the JSONObject (or XML) up to a certain nonnumerical path - done
820+
//Concert the replacement to a JSONObject
821+
//Merge the tw
822+
823+
//Need substring to take out first / so can use split successfully.
824+
String[] pathArray = path.toString().substring(1).split("[\\\\/]", -1);
825+
int pathSize = pathArray.length;
826+
827+
String replacementXML = toString(replacement);
828+
829+
JSONObject jo = new JSONObject();
830+
XMLParserConfiguration config = new XMLParserConfiguration();
831+
XMLTokener x = new XMLTokener(reader);
832+
833+
//Find the last non-numerical tag in pathArray
834+
int containsNumber = 0; //index of array where number is
835+
for(int i = 0; i < pathSize; i ++)
836+
{
837+
if(pathArray[i].matches("-?\\d+(\\.\\d+)?"))
838+
{
839+
containsNumber = i;
840+
}
841+
}
842+
843+
System.out.println("Contains number: " + containsNumber);
844+
845+
String parseTag;// = (containsNumber == 0) ? pathArray[pathSize - 1] : pathArray[containsNumber - 1];
846+
if(containsNumber == 0)
847+
{
848+
parseTag = pathArray[pathSize -1];
849+
}
850+
else
851+
{
852+
if(pathSize < 2)
853+
{
854+
parseTag = pathArray[0];
855+
}
856+
else
857+
{
858+
parseTag = pathArray[containsNumber - 1];
859+
}
860+
}
861+
String remArr = "";
862+
863+
//Need to get the new string array that can use JSONPointer to solve
864+
if(containsNumber != 0)
865+
{
866+
for(int z = containsNumber-1; z < pathSize; z ++)
867+
remArr += "/" + pathArray[z];
868+
}
869+
870+
System.out.println("remArr: " + remArr);
871+
System.out.println("Parse Tag: " + parseTag);
872+
873+
//rebuild the sub xml
874+
String rebuildXML = "";
875+
boolean cycle = true;
876+
877+
//boolean replacementPast = false;
878+
boolean firstPass = false;
879+
880+
String currTag ="";
881+
882+
String startString = "<" + parseTag;
883+
String exitString = "</" + parseTag;
884+
885+
//This is where we rebuild the XML
886+
while (x.more()) {
887+
x.skipPast("<");
888+
currTag = "<" + x.nextContent();
889+
//System.out.println(currTag);
890+
891+
892+
//Stop recording after start string reached.
893+
if(currTag.contains(startString))
894+
cycle = false;
895+
896+
897+
if(cycle)
898+
{
899+
if(firstPass)
900+
{
901+
// add the replacement string
902+
rebuildXML += replacementXML;
903+
904+
//turn it off
905+
firstPass = false;
906+
}
907+
908+
rebuildXML += currTag;
909+
//System.out.println("Pre If: " + currTag);
910+
}
911+
912+
//Start recording after the LAST non-numerical path string is reached.
913+
if(currTag.contains(exitString))
914+
{
915+
// if(containsNumber != 0)
916+
cycle = true;
917+
918+
//Mark that we have been through one iteration of the parse tag
919+
//Marking in case we need more in a JSON Array situation.
920+
firstPass = true;
921+
//else
922+
// break;
923+
}
924+
}
925+
926+
System.out.println(rebuildXML);
927+
JSONObject query = XML.toJSONObject(rebuildXML);
928+
System.out.println(query.toString());
929+
930+
if(containsNumber != 0)
931+
{
932+
Object jsquery = query.query(remArr);
933+
//System.out.println(jsquery);
934+
query = (JSONObject) jsquery;
935+
}
936+
937+
//System.out.println("PrintingJSON");
938+
939+
return query;
817940
}
818941

819942
/**

src/test/java/org/json/junit/XMLTest_SWE262.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,26 @@ public void testMethodOne() {
4646
//JSONObject jsonObject = XML.toJSONObject(xmlStr);
4747
//assertTrue("xml string should be empty", jsonObject.isEmpty());
4848
}
49+
50+
@Test
51+
public void testMethodTwo()
52+
{
53+
System.out.println("\nIn Test Method 2");
54+
55+
try {
56+
FileReader filereader = new FileReader("src/test/resources/Catalog.xml");
57+
JSONObject jo = XML.toJSONObject(filereader, new JSONPointer("/catalog/book"), XML.toJSONObject("<tname>rahul jain</tname>"));
58+
59+
System.out.println(jo);
60+
61+
System.out.println("Testing out JSON to XML Conversion");
62+
63+
System.out.println(XML.toString(jo));
64+
65+
} catch (FileNotFoundException e) {
66+
System.out.println("File not found");
67+
e.printStackTrace();
68+
}
69+
70+
}
4971
}

src/test/resources/Catalog.xml

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?xml version="1.0"?>
2+
<catalog>
3+
<book id="bk101">
4+
<author>Gambardella, Matthew</author>
5+
<title>XML Developer's Guide</title>
6+
<genre>Computer</genre>
7+
<price>44.95</price>
8+
<publish_date>2000-10-01</publish_date>
9+
<description>An in-depth look at creating applications
10+
with XML.</description>
11+
</book>
12+
<book id="bk102">
13+
<author>Ralls, Kim</author>
14+
<title>Midnight Rain</title>
15+
<genre>Fantasy</genre>
16+
<price>5.95</price>
17+
<publish_date>2000-12-16</publish_date>
18+
<description>A former architect battles corporate zombies,
19+
an evil sorceress, and her own childhood to become queen
20+
of the world.</description>
21+
</book>
22+
<book id="bk103">
23+
<author>Corets, Eva</author>
24+
<title>Maeve Ascendant</title>
25+
<genre>Fantasy</genre>
26+
<price>5.95</price>
27+
<publish_date>2000-11-17</publish_date>
28+
<description>After the collapse of a nanotechnology
29+
society in England, the young survivors lay the
30+
foundation for a new society.</description>
31+
</book>
32+
<book id="bk104">
33+
<author>Corets, Eva</author>
34+
<title>Oberon's Legacy</title>
35+
<genre>Fantasy</genre>
36+
<price>5.95</price>
37+
<publish_date>2001-03-10</publish_date>
38+
<description>In post-apocalypse England, the mysterious
39+
agent known only as Oberon helps to create a new life
40+
for the inhabitants of London. Sequel to Maeve
41+
Ascendant.</description>
42+
</book>
43+
<book id="bk105">
44+
<author>Corets, Eva</author>
45+
<title>The Sundered Grail</title>
46+
<genre>Fantasy</genre>
47+
<price>5.95</price>
48+
<publish_date>2001-09-10</publish_date>
49+
<description>The two daughters of Maeve, half-sisters,
50+
battle one another for control of England. Sequel to
51+
Oberon's Legacy.</description>
52+
</book>
53+
<book id="bk106">
54+
<author>Randall, Cynthia</author>
55+
<title>Lover Birds</title>
56+
<genre>Romance</genre>
57+
<price>4.95</price>
58+
<publish_date>2000-09-02</publish_date>
59+
<description>When Carla meets Paul at an ornithology
60+
conference, tempers fly as feathers get ruffled.</description>
61+
</book>
62+
<book id="bk107">
63+
<author>Thurman, Paula</author>
64+
<title>Splish Splash</title>
65+
<genre>Romance</genre>
66+
<price>4.95</price>
67+
<publish_date>2000-11-02</publish_date>
68+
<description>A deep sea diver finds true love twenty
69+
thousand leagues beneath the sea.</description>
70+
</book>
71+
<book id="bk108">
72+
<author>Knorr, Stefan</author>
73+
<title>Creepy Crawlies</title>
74+
<genre>Horror</genre>
75+
<price>4.95</price>
76+
<publish_date>2000-12-06</publish_date>
77+
<description>An anthology of horror stories about roaches,
78+
centipedes, scorpions and other insects.</description>
79+
</book>
80+
<book id="bk109">
81+
<author>Kress, Peter</author>
82+
<title>Paradox Lost</title>
83+
<genre>Science Fiction</genre>
84+
<price>6.95</price>
85+
<publish_date>2000-11-02</publish_date>
86+
<description>After an inadvertant trip through a Heisenberg
87+
Uncertainty Device, James Salway discovers the problems
88+
of being quantum.</description>
89+
</book>
90+
<book id="bk110">
91+
<author>O'Brien, Tim</author>
92+
<title>Microsoft .NET: The Programming Bible</title>
93+
<genre>Computer</genre>
94+
<price>36.95</price>
95+
<publish_date>2000-12-09</publish_date>
96+
<description>Microsoft's .NET initiative is explored in
97+
detail in this deep programmer's reference.</description>
98+
</book>
99+
<book id="bk111">
100+
<author>O'Brien, Tim</author>
101+
<title>MSXML3: A Comprehensive Guide</title>
102+
<genre>Computer</genre>
103+
<price>36.95</price>
104+
<publish_date>2000-12-01</publish_date>
105+
<description>The Microsoft MSXML3 parser is covered in
106+
detail, with attention to XML DOM interfaces, XSLT processing,
107+
SAX and more.</description>
108+
</book>
109+
<book id="bk112">
110+
<author>Galos, Mike</author>
111+
<title>Visual Studio 7: A Comprehensive Guide</title>
112+
<genre>Computer</genre>
113+
<price>49.95</price>
114+
<publish_date>2001-04-16</publish_date>
115+
<description>Microsoft Visual Studio 7 is explored in depth,
116+
looking at how Visual Basic, Visual C++, C#, and ASP+ are
117+
integrated into a comprehensive development
118+
environment.</description>
119+
</book>
120+
</catalog>

0 commit comments

Comments
 (0)